qt ver 5.14.2
qt Creator创建程序
Build System
- Qbs市场占有率低,将被弃用
- qmake更轻量级
- cmake功能强大
不繁琐的小程序用qmake,大工程文件用cmake更好
Details
Base class有三个选项:
- QMainWindow
- QWidget
- QDialog
QMainWindow和QDialog是QWidget的子类。QWidget创建出来只有一个空白界面;QMainWindow在QWidget基础上多了菜单栏,工具栏和状态栏;QDialog对应对话框。
Generate form选项可以通过拖拽完成界面的设计
Translation
Translation File功能显见
Kits
版本套件选择,例如:Desktop对应.exe开发;UWP对应windows通用平台开发;
qt程序组成
main.cpp
#include "mywidget.h"
#include <QApplication> // 包含一个应用程序类的头文件
//main程序入口;argc命令行变量的数量;argv命令行变量的数组
int main(int argc, char *argv[]){
//a应用程序对象,在Qt中,应用程序对象有且仅有一个
QApplication a(argc, argv);
//窗口对象,myWidget父类->QWidget
myWidget w;
//窗口对象 默认不会显示,必须要调用show方法才能显示窗口
w.show();
//让应用程序对象进入消息循环机制,窗口不会一闪而过
return a.exec();
}
.pro文件
QT += core gui //QT包含模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets //版本号大于4所额外包含模块
CONFIG += 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 \
mywidget.cpp
HEADERS += \ //头文件
mywidget.h
FORMS += \
mywidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
//防止被多次include,文件只会被编译一次和pragma once等效
#include <QWidget>//窗口类QWidget头文件
QT_BEGIN_NAMESPACE
namespace Ui { class myWidget; }
QT_END_NAMESPACE
class myWidget : public QWidget
{
Q_OBJECT //Q_OBJECT是一个宏,允许类中使用信号和槽的机制
public:
myWidget(QWidget *parent = nullptr);
~myWidget();
private:
Ui::myWidget *ui;
};
#endif // MYWIDGET_H
mywidget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
//命名规范
//类名 首字母大写,单词和单词之间首字母大写
//函数名 变量名称 首字母小写,单词和单词之间首字母大写
//快捷键
//注释 ctrl + /
//运行 ctrl + r
//编译 ctrl + b
//字体缩放 ctrl + 鼠标滚轮
//自动对齐 ctrl + i
myWidget::myWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::myWidget)
{
ui->setupUi(this);
}
myWidget::~myWidget()
{
delete ui;
}