使用Qt向导生成的应用程序.pro文件格式如下:
#-------------------------------------------------
#
# Project created by QtCreator 2022-02-16T13:07:45
#
#-------------------------------------------------
//模块的名字
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtTest //应用程序名
TEMPLATE = app //生成的makefile的模板类型
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
//源文件
SOURCES += \
main.cpp \
mainwindow.cpp
//头文件
HEADERS += \
mainwindow.h
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
RESOURCES += \
painer.qrc
.pro就是工程文件(project),它是qmake自动生成的用于生产makefile的配置文件。
.pro文件的写法如下:
注释:从“#”开始,到这一行结束。
模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择:TEMPLATE=app
- app–建立一个应用程序的makefile。这是默认值,所以如果模块没有被制定,这个将被使用。
- lib–建立一个库的makefile。
- vcapp–建立一个应用程序的VisualStudio项目文件。
- vclib–建立一个库的VisualStudio项目文件。
- subdirs–这是一个页数的模板,它可以创建一个能够进入特定目录并且为一个项目文件生成makefile并且为它调用make的makefile。
指定生成的应用程序名:
TARGET = QtDemo
#工程中包含的头文件:
HEADERS += mainwindow.h
#工程中包含的.UI设计文件:
FORMS += mainwindow.ui
#工程中包含的源文件:
SOURCES += main.cpp mainwindow.cpp
#工程中包含的资源文件:
RESOURCES += painer.qrc
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
这条 语句的含义是,如果 N QT_MAJOR_VERSION 大于 4 4 (也就是 当前使用的 的 Qt5及 更高版本) 需要 增加 s widgets 模块。如果 项目 仅需 支持 Qt5 , 也可以直接添加 “QT += widgets” 一句。不过为了保持代码 兼容 ,最好还是按照QtCreator 生成的语句编写。
CONFIG 用来告诉 qmake 关于应用程序的配置信息。
CONFIG += c++11 //使用 c++11 的特性
在这里使用“+=”,是因为我们添加我们的配置选项到任何一个已经存在中。这样做比使用“=”那样替换已经指定的所有选项更安全。