QT第二天学习

回顾:

qmake:

qmake -project //生成*.pro文件

qmake //makefile

make

 

注:if(qmake -v  >=5)

then QT += widgets

 

乱码:QTextCodec

几何属性:

位置大小

文档怎么看

 

------------------------------------

1.QT类的结构:按模块划分

core模块:所有的其它QT模块都依赖这个模块,默认加载。

常用的类:

容器类:QList  QString  QByteArray

几何类:QPoint QSize QRect

文件:QFile

目录:QDir

 

gui模块:包含了开发图形界面应用程序所需要的功能,默认加载

QColor QFont QImage QIcon QCursor...

 

widgets模块:提供了一个用于创建典型桌面应用程序的UI元素的集合。

窗体类 |-框架窗体类(QMainWindow,菜单,工具栏,状态栏)

|-对话框窗体类(QDialog模态对话框和非模态对话框)

 

组件:显示组件,输入组件,按钮,容器,布局,表格...

 

窗体类:

QObject QPaintDevice

| |

-------------

|

QWidget

|

-----------------------------

| | |

QMainWindow QDialog 控件

 

QObject:

connect();

tr();

QPaintDevice:

所有的可视化组件都是绘制设备

 

QWidget:

负责可视化组件的常规属性和基本事件

常规属性:

几何属性:高度,宽度,位置

图标,光标,标题...

基本事件:

鼠标事件

键盘事件

绘制事件

 

 

图标,光标:

光标:QCursor

图标:QIcon

 

2.布局

布局,layout,就是把各个控件放在合适的位置,占据适当大小的空间

 

绝对位置法:

手工布局:

可视化布局:

 

QT中,使用布局器QLayout进行布局

常用布局类:

QLayout

|-QBoxLayout

|-QHBoxLayout水平布局器

|-QVBoxLayout垂直布局器

|-QGridLayout网格布局

|-QFormLayout表单布局

 

伸展因子(器):俗称弹簧

addStretch(1);//参数是伸展力度

 

void QGridLayout::addWidget(

QWidget * widget,//需要添加的控件

int fromRow,//哪一行

int fromColumn,//哪一例

int rowSpan,//占几行

int columnSpan,//占几例

Qt::Alignment alignment = 0)//对齐方式

 

sizehint

 

代码一

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class myWidget : public QWidget
{
    Q_OBJECT
public:
    explicit myWidget(QWidget *parent = 0);
signals:
public slots:
};
#endif // MYWIDGET_H



mywidget.cpp
#include "mywidget.h"

myWidget::myWidget(QWidget *parent) :
    QWidget(parent)
{
}

main.cpp

#include <QApplication>
#include "mywidget.h"
#include <QIcon>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
myWidget w;
QCursor cursor(Qt::SplitHCursor);//创建一个光标对象
w.setCursor(cursor);//为窗体设置光标

QIcon ico("ico.png");//创建图标对象
w.setWindowIcon(ico);//为窗体设置图标

w.show();
return app.exec();
}

 

 

代码二

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class widget : public QWidget
{
Q_OBJECT
public:
explicit widget(QWidget *parent = 0);

signals:

public slots:

private:

QPushButton *btn1;
QPushButton *btn2;
QPushButton *btn3;
QPushButton *btn4;
QPushButton *btn5;
QPushButton *btn6;
QHBoxLayout *hbox;
QVBoxLayout *vbox;
QVBoxLayout *vbox1;

};

#endif // WIDGET_H

 


widget.cpp

#include "widget.h"

widget::widget(QWidget *parent) :
QWidget(parent)
{
btn1 = new QPushButton("btn1",this);
btn2 = new QPushButton("btn2",this);
btn3 = new QPushButton("btn3",this);

hbox = new QHBoxLayout;//创建水平布局器
hbox->addStretch(1);//添加一个弹簧
hbox->addWidget(btn3);//添加控件到布局器中
hbox->addWidget(btn2);//添加的顺序即显示的顺序
hbox->addWidget(btn1);
hbox->addStretch(1);
// this->setLayout(hbox);//设置(安装、应用)布局器到窗口

vbox = new QVBoxLayout;
vbox1 = new QVBoxLayout;
btn4 = new QPushButton("btn4");
btn5 = new QPushButton("btn5");
btn6 = new QPushButton("btn6");
vbox->addWidget(btn4);
vbox->addWidget(btn5);
vbox->addWidget(btn6);
vbox->addStretch(1);
// this->setLayout(vbox);

vbox1->addLayout(hbox);
vbox1->addLayout(vbox);
setLayout(vbox1);

}


main.cpp

#include <QApplication>
#include "widget.h"
int main(int argc,char **argv)
{

QApplication app(argc,argv);

widget w;
w.show();
return app.exec();
}

 

 

代码三

myqq.h

#ifndef MYQQ_H
#define MYQQ_H

#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class myQQ : public QWidget
{
Q_OBJECT
public:
explicit myQQ(QWidget *parent = 0);

signals:

public slots:

private:
QLabel *labelTile;
QLabel *labelUser;
QLabel *labelPwd;

QLineEdit *editUser;
QLineEdit *editPwd;

QPushButton *btnLogin;
QPushButton *btnRegister;

QHBoxLayout *hbox1;
QHBoxLayout *hbox2;
QHBoxLayout *hbox3;
QVBoxLayout *vbox;

};

#endif // MYQQ_H

 

 

myqq.cpp

#include "myqq.h"

myQQ::myQQ(QWidget *parent) :
QWidget(parent)
{
labelTile = new QLabel("myQQ");
labelTile->setAlignment(Qt::AlignCenter);
labelTile->setFont(QFont("ABC",20,40,true));
labelUser = new QLabel("user:");
labelPwd = new QLabel("passwd:");

editPwd = new QLineEdit;
editUser = new QLineEdit;

btnLogin = new QPushButton("Login");
btnRegister = new QPushButton("Register");

hbox1 = new QHBoxLayout;
hbox2 = new QHBoxLayout;
hbox3 = new QHBoxLayout;
vbox = new QVBoxLayout;

hbox1->addWidget(labelUser);
hbox1->addWidget(editUser);

hbox2->addWidget(labelPwd);
hbox2->addWidget(editPwd);

hbox3->addWidget(btnLogin);
hbox3->addWidget(btnRegister);

vbox->addWidget(labelTile);
vbox->addLayout(hbox1);
vbox->addLayout(hbox2);
vbox->addLayout(hbox3);

this->setLayout(vbox);
this->setFixedSize(300,200);
this->setWindowFlags(Qt::FramelessWindowHint);
}

 

main.cpp

#include <QApplication>
#include "myqq.h"
int main(int argc,char ** argv)
{

QApplication app(argc,argv);
myQQ mq;
mq.show();
return app.exec();

 

代码四

#include <QApplication>
#include <QGridLayout>
#include <QComboBox>
#include <QWidget>
#include <QFormLayout>
#include <QLineEdit>
#include <QSpinBox>
int main(int argc,char ** argv)
{
QApplication app(argc,argv);
QComboBox cb1,cb2,cb3,cb4,cb5;
cb1.addItem("ABC");
cb1.addItem("CBA");
cb1.addItem("NBA");
// QGridLayout glayout;
// glayout.addWidget(&cb1,0,0,1,1);
// glayout.addWidget(&cb2,0,1,1,1);
// glayout.addWidget(&cb3,1,0,1,1);
// glayout.addWidget(&cb4,1,1,1,1);
// glayout.addWidget(&cb5,2,0,1,2);

QWidget w;
// w.setLayout(&glayout);
QLineEdit *nameLineEdit = new QLineEdit;
QLineEdit *emailLineEdit = new QLineEdit;
QSpinBox *ageSpinBox = new QSpinBox;
QFormLayout *formLayout = new QFormLayout;
formLayout->addRow("&Name:", nameLineEdit);
formLayout->addRow("&Email:", emailLineEdit);
formLayout->addRow("&Age:", ageSpinBox);
w.setLayout(formLayout);

w.show();

return app.exec();
}

 

 

 

 

我的qq界面设计代码

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);

signals:

public slots:

private:
    QPushButton *btn1;
    QPushButton *btn2;
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLineEdit *line1;
    QLineEdit *line2;
    QHBoxLayout *hbox;
    QHBoxLayout *hbox1;
    QHBoxLayout *hbox2;
    QHBoxLayout *hbox3;
    QVBoxLayout *vbox1;
};

#endif // WIDGET_H



widget.cpp
#include "widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    btn1=new QPushButton("登陆",this);
    btn2=new QPushButton("注册",this);
    label1=new QLabel("QQ 2116");
    label2=new QLabel("用户名");
    label3=new QLabel("密码");
    line1=new QLineEdit;
    line2=new QLineEdit;

    hbox = new QHBoxLayout;
    hbox->addStretch(1);
    hbox->addWidget(label1);
    hbox->addStretch(1);

    hbox1 = new QHBoxLayout;
    hbox1->addStretch(1);
    hbox1->addWidget(label2);
    hbox1->addWidget(line1);
    hbox1->addStretch(1);

    hbox2 = new QHBoxLayout;
    hbox2->addStretch(1);
    hbox2->addWidget(label3);
    hbox2->addWidget(line2);
    hbox2->addStretch(1);

    hbox3 = new QHBoxLayout;
    hbox3->addStretch(1);
    hbox3->addWidget(btn1);
    hbox3->addWidget(btn2);
    hbox3->addStretch(1);

    vbox1 = new QVBoxLayout;
    vbox1->addLayout(hbox);
    vbox1->addLayout(hbox1);
    vbox1->addLayout(hbox2);
    vbox1->addLayout(hbox3);
    setLayout(vbox1);
    this->setFixedSize(300,200);
}


main.cpp
#include <QApplication>
#include "widget.h"

int main(int argc,char **argv)
{
    QApplication app(argc,argv);
    Widget w;
    QCursor cursor(Qt::PointingHandCursor);//创建一个光标对象
    w.setCursor(cursor);//为窗体设置光标
    QIcon ico("ico.png");//创建图标对象
    w.setWindowIcon(ico);//为窗体设置图标
    w.show();
    return app.exec();
}


posted @ 2016-07-19 19:48  立刻行动  阅读(645)  评论(0编辑  收藏  举报