Qt详解

本文介绍了使用QT创建GUI程序的基础知识,包括QApplication、QWidget、QPushButton、QLabel、QLineEdit等类的使用方法,详细解释了如何创建窗口、添加按钮、标签、文本输入框,并演示了信号与槽的关联,提供了具体的代码实例。

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/archive/2013/01/04/2844531.html

### Python QT详解及使用指南 #### 1. QT与Python的结合:PyQt和PySide 在Python中,QT框架主要通过PyQt或PySide实现。两者都提供了对QT功能的全面支持,包括GUI组件、网络通信、数据库访问等[^1]。 ```python # PyQt 示例代码 from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) label = QLabel('Hello, PyQt!') label.show() app.exec_() ``` #### 2. 基本组件与布局管理 QT提供了一系列UI组件,如按钮、文本框、列表框等,并支持多种布局方式(水平、垂直、网格等)以灵活调整界面元素的位置和大小。 ```python # 使用布局管理器 from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton app = QApplication([]) window = QWidget() layout = QVBoxLayout() button1 = QPushButton('Button 1') button2 = QPushButton('Button 2') layout.addWidget(button1) layout.addWidget(button2) window.setLayout(layout) window.show() app.exec_() ``` #### 3. 信号与槽机制 信号与槽是QT的核心特性之一,用于实现对象间的通信。当某个事件发生时,信号会被发出并触发关联的槽函数执行相应操作[^1]。 ```python # 连接信号与槽 from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QApplication, QPushButton @pyqtSlot() def on_button_click(): print('Button clicked!') app = QApplication([]) button = QPushButton('Click me') button.clicked.connect(on_button_click) button.show() app.exec_() ``` #### 4. 数据模型与视图 QT的数据展示采用模型-视图架构,其中数据由模型维护,而视图负责呈现给用户。这种分离提高了代码的可维护性和重用性。 ```python # 简单的模型-视图示例 from PyQt5.QtCore import QStringListModel from PyQt5.QtWidgets import QApplication, QListView app = QApplication([]) model = QStringListModel(['Item 1', 'Item 2', 'Item 3']) view = QListView() view.setModel(model) view.show() app.exec_() ``` #### 5. 多线程与异步处理 为避免界面卡顿,复杂任务应放在独立线程中运行。QT提供了QThread类来简化多线程编程。 ```python # 使用QThread进行后台任务 from PyQt5.QtCore import QThread, pyqtSignal import time class Worker(QThread): progress = pyqtSignal(int) def run(self): for i in range(101): time.sleep(0.1) self.progress.emit(i) app = QApplication([]) worker = Worker() worker.start() app.exec_() ``` #### 6. 资源文件与国际化 通过QT资源系统,可以将图片、图标等嵌入到应用程序中。同时,借助翻译工具,能够轻松实现应用的多语言支持。 ```python # 加载资源文件中的图标 from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QPushButton app = QApplication([]) button = QPushButton() button.setIcon(QIcon(':/icons/myicon.png')) button.show() app.exec_() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值