Skip to content
On this page

QT简介


标签:CPP/QT  

QT简介

  • 基于 C++ 编写的图形开发库
  • 使用场合:汽车仪表盘,打印机,医疗器械,自动化大型设备……
  • 优点:跨平台,图形库完备;接口封装性好,易上手;有一套简易好用的内存回收机制;社区友好;轻量级开发,支持嵌入式
  • 下载地址:Index of /archive/qt
  • 工具介绍:(E:\Qt\Qt5.14.2\5.14.2\mingw73_64\bin)
    • Assistant.exe :帮助手册
    • Designer.exe:图形化开发,对应文件为 .ui 文件
    • uic.exe:将 .ui 转换成编译器可读的 .h 文件,uic xxx.ui -o ui_xxx.h
    • moc.exe:将 QT 中非标准的信号与槽,转换成标准的C++代码
    • rcc.exe:将 QT 资源文件(图片,音频,视频),转换为标准的 C++ 代码
    • qmake.exe:工程管理工具,将 .pro 的工程文件生成 makefile 文件,通过 makefile 编译 C++ 代码
    • QTCrteator:QT 的集成开发环境,包含了上述工具

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 += target
cpp
#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_H
cpp
#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();                 // 阻塞等待界面处理,等待信号与槽、
                                   // 等待用户操作、等待事件发生
}

Last updated: