Appearance
QT简介
- 基于 C++ 编写的图形开发库
- 使用场合:汽车仪表盘,打印机,医疗器械,自动化大型设备……
- 优点:跨平台,图形库完备;接口封装性好,易上手;有一套简易好用的内存回收机制;社区友好;轻量级开发,支持嵌入式
- 下载地址:Index of /archive/qt
- 工具介绍:(
E:\Qt\Qt5.14.2\5.14.2\mingw73_64\bin)
QT项目文件介绍
- 新建一个名为 test 的 MainWindow 项目,有如下几个文件
yaml
QT += core gui
# 工程项目包含的类库:core核心库,gui 图形化界面库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 4.0 版本后,自动添加 widgets 库(4.0 版本前包含在 core 库中)
CONFIG += c++11
# 支持 C++11 标准
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# 管理源文件
SOURCES += \
main.cpp \
mainwindow.cpp
# 管理头文件
HEADERS += \
mainwindow.h
# 管理 ui 界面文件
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetcpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
// 防止文件重复包含
#include <QMainWindow> // 父类的头文件
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } // 命名空间的声明,ui界面生成的头文件中的命名空间
QT_END_NAMESPACE
class MainWindow : public QMainWindow { // 自定义类,继承自 QMainWindow 类
Q_OBJECT // 处理信号和槽的元对象
public:
MainWindow(QWidget *parent = nullptr); // 构造函数声明
~MainWindow(); // 析构函数声明
private:
Ui::MainWindow *ui; // 指向 ui 界面的指针,designer 中添加组件需要该指针找到
};
#endif // MAINWINDOW_Hcpp
#include "mainwindow.h" // 对应的头文件
#include "ui_mainwindow.h" // mainwindow.ui 生成的头文件
MainWindow::MainWindow(QWidget *parent) // 构造函数的实现
: QMainWindow(parent) // 调用父类构造函数
, ui(new Ui::MainWindow) // 给指针成员进行初始化
{
ui->setupUi(this); // 调用 designer 中设置的参数
}
MainWindow::~MainWindow() // 析构函数定义
{
delete ui; // 将类中成员时针释放
}cpp
#include "mainwindow.h" // 引入自定义头文件
#include <QApplication> // 引入应用程序的头文件
int main(int argc, char *argv[]) { // 主函数
QApplication a(argc, argv) // 实例化应用程序对象
MainWindow w; // 使用自定义对象在栈区实例化一个对象
w.show(); // 调用 show 显示界面
return a.exec(); // 阻塞等待界面处理,等待信号与槽、
// 等待用户操作、等待事件发生
}