Qt5学习笔记2:Qt (Creator)工程简介-- -xxx.pro、xxx.ui文件及main()

本文对一个简单的Hello world工程进行解析,从而对Qt工程项目有一个总体的认识。

在上篇Qt5教程1中创建了一个简单的Hello world工程,如图:

Qt5学习笔记2:Qt (Creator)工程简介-- -xxx.pro、xxx.ui文件及main()_第1张图片

本文主要从xxx.pro文件、xxx.ui文件、main()函数进行解析。

 

一、xxx.pro文件

如helloQt.pro文件,是一个project文件,是Qt项目的管理文件,用于记录项目的一些设置、文件组织管理等。

代码如下:

#-------------------------------------------------
#
# Project created by QtCreator 2019-09-08T12:21:47
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = helloQt
TEMPLATE = app

# 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

说明:

代码 作用
QT += core gui core是QT的核心/基础代码模块,gui是图形界面库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 判断版本,如果>4则加入widgets模块(widgets提供一些UI元素,布局和控件)
TARGET = helloQt 指定生成的目标文件
TEMPLATE = app 使用的模板是app,告诉qmake生成哪种Makefile
DEFINES += QT_DEPRECATED_WARNINGS DEFINES定义编译时的宏
CONFIG += c++11 指定编译器选项和项目配置,使用c++11标准编译
SOURCES += 指定编译的源文件(.cpp)
HEADERS += 指定编译的头文件(.h)
FORMS += 指定编译的ui文件(.ui)

 

 

二、xxx.ui文件

如mainwindow.ui文件,是一个界面文件,可视化设计的窗体的定义文件,可对界面进行设计、布局组件等。

双击打开ui文件,如图:

Qt5学习笔记2:Qt (Creator)工程简介-- -xxx.pro、xxx.ui文件及main()_第2张图片

中间的是设计窗口,可将各组件放置上面,进行布局等设计;

左侧栏这组件面板,有各种组件,共分以下几类:

名称 说明
Layouts 布局控件,有水平/垂直/网格等布局,用于窗体排版,不显示
Spacers 间隔器,提供水平/垂直2种,用于控制组件间的相对位置,不显示
Buttons 按钮组件
Item Views (Model-Based) 单元视图,包含列表、树形、表格等图表
Item Widgets (Item-Based) 单元组件,有列表、树形、表格等单元控件
Containers 容器类控件,有组合框、控件栈。。。等
Input Widgets 输入类控件,包含各种编辑框
Display Widgets 显示类控件,包含各种显示框、各种线条。。。等

 

三、main函数

在Hello world工程中,main()函数代码如下:

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

解析:

QApplication是Qt的标准应用程序类,QApplication a(argc, argv);定义一个实例a;

MainWindow w;定义一个MainWindow类的实例w,是主窗口;

w.show();显示该窗口;

a.exec();启动该应用程序的执行,开始消息循环和事件处理等。

 

完~

 

你可能感兴趣的:(qt)