Qt5學習筆記2:Qt (Creator)工程簡介-- -xxx.pro、xxx.ui文件及main()

本文對一個簡單的Hello world工程進行解析,從而對Qt工程項目有一個總體的認識。

在上篇Qt5教程1中創建了一個簡單的Hello world工程,如圖:

本文主要從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文件,如圖:

中間的是設計窗口,可將各組件放置上面,進行佈局等設計;

左側欄這組件面板,有各種組件,共分以下幾類:

名稱 說明
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 <QApplication>

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();啓動該應用程序的執行,開始消息循環和事件處理等。

 

完~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章