Qt详解

QT是一个创建GUI程序的C++类库
QApplication,头文件<qapplication.h>
QWidget,头文件<qwidget.h>
QPushButton,头文件<qpushbutton.h>
QLabel,头文件<qlabel.h>
QLineEdit,头文件<qlineedit.h>
//头文件QApplication,QWidget,QPushButton类的声明
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
int main(int argc,char *argv[])
{
QApplication a(argc,argv);//创建QApplication对象
QWidget mainwindow; //创建QWidget对象,相当于一个窗口,可以在其上放置其他对象
mainwindow.setMinimumSize(200,100);//设置窗口最小尺寸
mainwindow.setMaximumSize(800,200);//设置窗口最大尺寸,如果最小尺寸与最大尺寸设得一样,则不能再调整窗口大小
QPushButton helloworld("hello,QT!",&mainwindow);//创建按钮对象并直接调用构造函数,第二个参数指明按钮的父窗口
helloworld.setGeometry(20,20,160,60);//设置按钮的几何尺寸,前两参数为其在父窗口的位置,后两个参数的按钮大长宽
a.setMainWidget(&mainwindow);//告诉QApplication对象将mainwindow设为程序的主部件
mainwindow.show();//显示父部件则子部件已一块显示出来
return a.exec();//将控制权转交给QT,由QT接收和处理事件,将传递给相应的窗口
}

qmake -project
qmake
make
在类的定义中将数据成员定义为指针变量,在类的构造函数中用new进行动态内存申请,在析造函数中用delete删除

在进行QT设计程序时应尽量使用已有的类
类的继承
#include<qapplication.h>
#include<qwidget.h>
class myclass:public QWidget
{
public:
myclass();
};
myclass::myclass()
{
this->setMinimumSize(200,200);
this->setMaximumSize(200,200);
}
int main(int arc,char*argv[])
{
QApplication a(argc,argv);
myclass w;
a.setMainWidget(&w);
w.show();
return a.exec();
}
面向对象的QT设计方法OOP
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
//first,describe the class,which method and other members it shall include
class myclass:public QWidget
{
public:
myclass();
private:
QPushButton *b1;
};

myclass::myclass()
{
setMinimumSize(200,200);
setMaximumSize(200,200);
b1=new QPushButton("Hello",this);
b1->setGeometry(20,20,160,60);
}
int main(int argc,char *argv[])
{
QApplication a(argc,argv);
myclass w;
a.setMainWidget(&w);
w.show();
return a.exec();
}
QWidget主部件,相当于一个界面或窗口,可以把其他子部件(按钮,滚动条,标签等)全添加到主部件上,一个程序只能有一个主部件,当主部件终止时整个程序也就结束了。
方法:
QWidget和QDialog
QWidget::setGeometry()定义窗口首次大小和显示位置
如setGeometry(100,100,200,120);
前两个参数是位置,后两个参数是大小
在主部件中添加对象
添加按钮QPushButton类
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
QPushButton *b1;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,200,120);
b1=new QPushButton("Button1",this);
b1->setGeometry920,20,180,80);
b1->setFont(QFont("Times",18,QFont::Bold));
}
int main(int argc,char *argv[])
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
格式化文本
QPushButton::setFont()
QFont(字体,大小,加粗,斜体)
b1->setFont(QFont("courier",12,QFont::Light,TRUE));

QFont font("Times",16,QFont::Bold);
b1->setFont(font);
添加标签
QLabel类头文件<qlabel.h>
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlabel.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
QPushButton *b1;
QLabel *label;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,200,170);
b1=new QPushButton("Button1",this);
b1->setGeometry(20,20,100,80);
b1->setFont(QFont("Times",18,QFont::Bold));
label=new QLabel(this);
label->setGeometry(20,110,180,50);
label->setText("This is the first line\nThis is the second line");
label->setAlignment(AlignCenter);
}
void main(int argc,char **argv)
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
交互方式
其他大多GUI库使用回调函数方式
QT使用信号和槽
信号和槽是相互关联的函数,信号(Signal,实际上为一函数)执行时,与该信号相连接的槽(Slog,实际也为一函数)也得以执行.
将b1按钮的clicked()信号与qApp的quit()槽相连接
当点击按钮时将产生QPushButton::clicked()信号,导致qApp的quit()槽被执行,程序退出
qApp为QT内置指针,总是指向程序中QApplication对象
qApp---------->>>>>QApplication
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlabel.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
QPushButton *b1;
QLabel *label;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,200,170);
b1=new QPushButton("Button1",this);
b1->setGeometry(20,20,100,80);
b1->setFont(QFont("Times",18,QFont::Bold));
label=new QLabel(this);
label->setGeometry(20,110,180,50);
label->setText("This is the first line\nThis is the second line");
label->setAlignment(AlignCenter);
connect(b1,SIGNAL(clicked()),qApp,SLOT(quit());//将clicked()信号与quit()槽相关联
}
void main(int argc,char **argv)
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
将用户事件(单击)与系统事件(退出)关联起来
QT中只需要一行代码就可以将用户事件与系统事件关联起来
MOC:Meta Object Compiler元对象编译器用于构造用户自己的信号和槽
槽就是普通的成员函数,但是它们可以连接到信号
每当槽所连接的信号被发射时,槽就被执行
槽实际上是标准的函数,可以像调用苊他函数一样调用它们
类QSlider,头文件<qslider.h>
类QLCDNumber,头文件<qlcdnumber.h>
#include <qapplication.h>
#include <qwidget.h>
#include <qfont.h>
#include <qpushbutton.h>
#include <qlcdnumber.h>
#include <qslider.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
QPushButton *b1;
QLCDNumber *lcd;
QSlider *slider;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,300,200);
b1=new QPushButton("Quit",this);
b1->setGeometry(10,10,80,40);
b1->setFont(QFont("Times",18,QFont::Bold));
lcd=new QLCDNumber(2,this);
lcd->setGeometry(100,10,190,180);
slider=new QSlider(Vertical,this);
slider->setGeometry(10,60,80,130);
connect(b1,SIGNAL(clicked()),qApp,SLOT(quit());
connect(slider,SIGNAL(valueChanged(int)),lcd,SLOT(display(int));
}
void main(int argc,char *argv[])
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
将b1的clicked()信号与qApp的quit()槽连接
将slider的valueChanged()信号与lcd的display()槽相连接
用三个QPushButton对象控制是否选定,取消选定,删除一个QLineEdit对象中的文本
通过将QPushButton的clicked()信号连接到QLineEdit对象适当的槽来实现
#include <qapplication.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qfont.h>
#include <qlineedit.h>
#include <qstring.h>
class MyMainWindow:public QWidget
{
public:
MyMainWindow();
private:
QPushButton *b1;
QPushButton *b2;
QPushButton *b3;
QLineEdit *ledit;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100,100,300,200);
b1=new QPushButton("Click here to mark the text",this);
b1->setGeometry(10,10,200,40);
b1->setFont(QFont("Times",18,QFont::Bold));

b2=new QPushButton("Click here to unmark the text",this);
b2->setGeometry(10,60,200,40);
b2->setFont(QFont("Times",18,QFont::Bold)):

b3=new QPushButton("Click here to remove the text",this);
b3->setGeometry(10,110,200,40);
b3->setFont(QFont("Times",18,QFont::Bold));

ledit=new QLineEdit("This is the line of text",this):
ledit->setGeometry(10,160,280,30);
connect(b1,SIGNAL(clicked()),ledit,SLOT(selectAll()));
connect(b2,SIGNAL(clicked()),ledit,SLOT(deselect()));
connect(b3,SIGNAL(clicked()),ledit,SLOT(clear()));
}
void main(int argc,char *argv[])
{
QApplication a(argc,argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}



原文地址:https://www.cnblogs.com/hainanlinyu/p/2844531.html