Skip to content

Wiki

QT Environment Setup

About 4 min read

1.Install QT

1.1.QT5


# debian系
sudo apt install qt5-default          # 基础开发工具(qmake、moc 等)
sudo apt install qtbase5-dev          # Qt5 核心库开发文件
sudo apt install qttools5-dev         # Qt5 工具(Qt Designer、Linguist 等)

# 红帽系

# 安装 Qt5 核心开发包
sudo dnf install qt5-qtbase-devel      # Qt5 核心库开发文件
sudo dnf install qt5-qttools-devel     # Qt5 工具(Qt Designer、Linguist 等)

# 安装常用模块(按需选择)
sudo dnf install \
  qt5-qtdeclarative-devel \           # Qt Quick
  qt5-qtsvg-devel \                   # SVG 支持
  qt5-qtwayland-devel \               # Wayland 支持
  qt5-qtwebengine-devel               # WebEngine 支持

1.2.QT6

https://www.qt.io/product/qt6


# debian系
sudo apt install qt6-base-dev qt6-tools-dev

# 红帽系
sudo dnf install qt6-qtbase-devel qt6-qttools-devel

sudo dnf install qt6-qtdeclarative-devel qt6-qtsvg-devel qt6-qtwayland-devel qt6-qt5compat-devel qt6-qtwebsockets-devel

2.VSCode environment configuration

Mainly using CMake to set up the QT5/QT6 development environment; seeCMake C/C++ compilation environment configuration

3.QT Designer generates .ui

The main approach is to use the software below for graphical design, then generate.uifiles and then convert them into.hfiles for use in the C/C++ project.

For example, we create a Helloworld window; after opening QT Designer, choose to create a Widget.

Drag it in and type Hello World!

You can adjust the font size.

The objectName can be modified, which is the class name called in the C++ code.

Finally, save the .ui file, generally saved in the form folder under the function package.

4.Call the .ui class and compile and run

First, make sure your VScode+CMake is configured correctly.

Then cmake .., then make install, At this point QT_Projects/QT6/QT6_Template/build/src/QT6TEST/ will appear in the directory .h file.

Then this .h can be referenced in the code.

Then you can proceed to implement your own code functionality.

#include "QT6TEST/inc/qt6_test.hpp"
#include <QApplication>
#include <QWidget>
#include "ui_mywidget.h"

int qt6_test(int argc,char **argv)
{
    QApplication app(argc, argv);

    // 创建主窗口和 UI 对象
    QWidget mainWindow;
    Ui::MyWidget ui;        // Ui 命名空间中的类名与 .ui 文件中的 class 属性一致
    ui.setupUi(&mainWindow);

    // 设置窗口标题
    mainWindow.setWindowTitle("Hello Qt6!");

    // 显示窗口
    mainWindow.show();

    return app.exec();
}

I have a configured QT6 environment here, and you can clone it for use.

https://github.com/tungchiahui/QT_Projects/tree/main/QT6/QT6_Template