Learn QT's solution and project structure

This article uses QT5 to create the project, the specific version is QT5.14.2, and the included QT Creator is 4.11.1

1. Create a default QT project

1. Open QT Creator (4.11.1 Community Edition), File -> New:

image-202205031743525

2. Set the project name and select the storage path:

image-20220503220815575

3. Select Build System

The options here are:

  • CMake :
  • qmake :
  • Qbs :

The default is used here.image-20220503174457898

4. Set the information of the main window:

  • class name 及 base class
  • header file 及 source file
  • Main window ui : mainwindow.ui

image-20220503174654805

5. Choose to use local language by default

"Simplified Chinese" is selected here, and the configuration can be added later

image-20220503221151462

6. Select the compilation kit Kits:

image-20220503221222185

7. Finally preview the project, you can choose git as the version selection

image-20220503221243586

After completion, it defaults to the editing main.cppinterface:

image-20220503233226039

Click Run or Ctrl + R in the lower left corner to run the project, and you can see an App with an empty interface:

image-20220503181200390

2. Project directory and files

In the "Edit" column, you can see the project directory and files:

image-20220503233247821

An overview is as follows:

FirstQt5 : 项目名称及git分支

- CMakeList.txt : CMakeList内容
- FirstQt5 : 工程名称
  - Header Files : 头文件目录
    - mainwindow.h : 主窗口头文件
  - Source Files : 源文件目录
    - main.cpp : QT App应用入口源文件
    - mainwindow.cpp : 主窗口源文件
  - FirstQT_zh_CN.ts : (此文件在文件管理中可看到)项目本地化语言文件,此处为 zh_CN 简体中文文件
  - mainwindow.ui : 主界面的ui, 可使用界面化鼠标操作
- CMake Modlues : CMake 模块 QT安装目录
  - <Other Location>
    - C:\QT

If we do not check "Generate form" when creating a project, as shown below:

image-20220504183757825

Then the project structure is as follows, one is missing mainwindow.ui,

image-20220504184034842

2.1 CMakeList.txt file

The content of the file is as follows, and the content of the file is directly explained below using comments.

# cmake 最低要求版本 3.5, 测试电脑安装为 3.23.1
cmake_minimum_required(VERSION 3.5)
# 项目配置
project(FirstQt5 LANGUAGES CXX)
# CMAKE配置项: 包含当前目录 on
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# CMAKE配置项: 是否为Qt目标自动处理 uic 
set(CMAKE_AUTOUIC ON)
# CMAKE配置项: 是否为Qt目标自动处理 moc
set(CMAKE_AUTOMOC ON)
# CMAKE配置项: 是否为Qt目标自动处理 rcc
set(CMAKE_AUTORCC ON)
# 要求构建这个目标的C++标准的特征版本 C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
# They need to be set before the find_package(Qt5 ...) call.

#if(ANDROID)
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
#    if (ANDROID_ABI STREQUAL "armeabi-v7a")
#        set(ANDROID_EXTRA_LIBS
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
#            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
#    endif()
#endif()

# 查找package
find_package(Qt5 COMPONENTS Widgets LinguistTools REQUIRED)
# 设置变量 TS_FILES 为 FirstQt5_zh_CN.ts 语言文件
set(TS_FILES FirstQt5_zh_CN.ts)
# 添加包含的文件
if(ANDROID)
  add_library(FirstQt5 SHARED
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${TS_FILES}
  )
else()
  add_executable(FirstQt5
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    ${TS_FILES}
  )
endif()
# 将目标文件与库文件进行链接
target_link_libraries(FirstQt5 PRIVATE Qt5::Widgets)
# 处理给定的源文件(目录或单个文件)以生成Qt Linguist .ts文件, 其中 CMAKE_SOURCE_DIR 为工程顶层目录
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})

2.2 Source Files

The source file contains two:

  • main.cpp : main function entry of qt application
  • mainwindow.cpp : the main window of the application

Where main.cpp is as follows:

// mainwindow.h 文件是我们自己创建的,引入时用" "双引号括起来
#include "mainwindow.h"
// QApplication 是 Qt 提供给我们的,引入时用<>括起来
#include <QApplication>
// main() 函数是应用程序的入口。它的主要功能是创建应用程序,创建窗口,显示窗口,并运行应用程序,开始应用程序的消息循环和事件处理。
// argc : 命令行变量的数量
// argv : 命令行变量数组
int main(int argc, char *argv[])
{
    // 定义并创建应用程序
    QApplication a(argc, argv);
    // 定义并创建窗口
    MainWindow w;
    // 显示窗口
    w.show();
    // 应用程序运行, 开始应用程序的消息循环和事件处理
    return a.exec();
}

2.3 mainwindow code

If you did not check to create ui when creating the project, then mainwindow.hand mainwindow.cppthe content is as follows:

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
    
    
    // 定义好的宏 Q_OBJECT, 所有需要“信号和槽”功能的组件都必须将 Q_OBJECT 作为 private 属性成员引入到类中
    Q_OBJECT
public:
    //带参的构造函数:QWidget 是所有组件的基类,借助 parent 指针,可以为当前窗口指定父窗口
    MainWindow(QWidget *parent = 0);
    // 无参构造函数
    ~MainWindow();
};
#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
    
}
MainWindow::~MainWindow()
{
    
    
}

When creating a project, check to create ui, and mainwindow.hthe mainwindow.cppcontent is as follows:

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
    
     class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    
    
    // 定义好的宏 Q_OBJECT, 所有需要“信号和槽”功能的组件都必须将 Q_OBJECT 作为 private 属性成员引入到类中
    Q_OBJECT

public:
     //带参的构造函数:QWidget 是所有组件的基类,借助 parent 指针,可以为当前窗口指定父窗口
    MainWindow(QWidget *parent = nullptr);
    // 无参构造函数
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

//mainwindow.ui

Compared with the project without ui above, there are more mainwindow.uidefinitions, among which mainwindow.uiis the ui design window that can be operated by the mouse,

image-20220504185107475

The code part is as follows, the code part cannot be edited directly, but can only be modified using the designer.

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Folding the code we get the following:

image-20220811195535788

The following information can be obtained from the code section:

  1. ui tag : Indicates that the current file is the layout information file of ui and the version used
  2. class tag : Indicates the class name corresponding to the current layoutMainWindow
  3. widget tag : Indicates a widget, where class is its type QWindow, name is its name MainWindow, and it contains three sub-widgets:
    • QWidget
    • QMenuBar
    • QStatusBar
  4. property tag : Indicates a property information. In the widget, the and properties MainWindoware set , corresponding to the geometry of the QWidget class of the QMainWindow parent class : QRect and windowTitle : QStringgeometrywindowTitle
  5. resources tag : Indicates the imported resources
  6. connections tag : Indicates the content of the link

3. Summary

So far, the default project structure created by QT is understood, and QT development will be further studied in the future.

This article is first published on blog.devwiki.net , welcome to visit.

Guess you like

Origin blog.csdn.net/DevWiki/article/details/127808475