QT项目文件的组成

QT项目文件的组成

我新建项目的文件结构截图,下面将对文件各部分功能进行简要介绍,本博客用于学习、记录。

1.项目管理文件

.pro是项目的管理文件,文件名就是项目的名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
QT       += core gui//表示项目加入core gui模块,core gui是qt使用gui设计的类库模块,如果创建控制台程序,则不需要添加core gui
//qt类库以模块的形式组织各种功能的类,根据涉及项目的功能需求,在项目中 添加适当的类库模块支持即可。例如,涉及数据库操作就加上这么一行:Qt +=sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets//当Qt版本大于4时,加入widgets模块

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 \
mainwindow.cpp

HEADERS += \
mainwindow.h

FORMS += \
mainwindow.ui

TRANSLATIONS += \
sampl_1_zh_CN.ts

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2.界面文件

.ui文件是可视化设计的窗体的定义文件,双击项目可使用Qt Designer对窗体进行可视化设计

以下是界面的简单说明

3.主函数文件

main.cpp,以下是其中的内容

1
2
3
4
5
6
7
8
9
10
11
#include "widget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv); //定义并创建应用程序
MainWindow w; //定义并创建窗口
w.show(); //显示窗口
return a.exec(); //应用程序运行
}

4.窗体类相关的文件

① .h文件

在其中定义了一个继承来自QWidget的类Widget

②.cpp(不是main.cpp)

是widget的实现代码

③.ui文件

.ui是一个窗体界面定义文件,是一个xml文件,定义了窗口上的所有组件的属性设置、布局以及信号与槽函数的关联等。