1. QT环境
1.1 QT_CREATOR
QT的集成开发工具,可以进行项目的创建运行。有一些实例可以运行之。
1.2 QT_ASSISTANT
QT的工具书
2. 核心的概念
2.1 windows
窗口
2.2 widget
组件放置在窗口上的
2.3 bar
栏
2.4 icon
图标
3. Hello, QT
3.1 main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
返回值与普通的C++程序不同的地方是,返回的是QApplication.exec()
, 这是一个阻塞的循环,一直接受消息。
3.2 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Q_OBJECT
是一个宏定义,当使用信号槽时,则需要加上,而且是在private
属性下加(CPP默认成员属性为私有)。
3.3 mainwindow.cpp
主窗口的构造析构函数实现
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);}MainWindow::~MainWindow()
{delete ui;
}
3.4 HelloQt.pro
qt
是一个CPP的图形界面框架,当然可以用cmake
; 但也提供了自己的工具qmake
。
QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \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