使用clion工具编写cpp qt demo程序
Published in:2024-08-09 |
Words: 858 | Reading time: 5min | reading:

使用clion工具编写cpp qt demo程序

qt install (windows 平台)

  • website
1
https://doc.qt.io/qt-5/windows.html
  • mirror config
1
https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/online_installers/

use tools install all components

配置文件

CmakeList.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
cmake_minimum_required(VERSION 3.26)
project(untitled2)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "C:/Qt/6.7.2/mingw_64/lib/cmake")

find_package(Qt6 COMPONENTS
Core
Gui
Widgets
REQUIRED)

add_executable(untitled2 main.cpp)
target_link_libraries(untitled2
Qt::Core
)

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(DEBUG_SUFFIX)
if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB Core)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()

cpp code

1
2
3
4
5
6
7
8
9
#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
qDebug() << "Hello World";

return QCoreApplication::exec();
}

If exists problem, add follow environment to path

1
2
C:\Qt\6.7.2\mingw_64
C:\Qt\Tools\QtCreator\bin

other demo

clion create cpp qt demo

follow pic
img

  • 通过uic创建ui_file.h头文件, 配置如图
    img

qt ui cpp demo

  • cpp ui(main_ui.cpp)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //
    // Created by Administrator on 2024/8/16 0016.
    //

    // You may need to build the project (run Qt uic code generator) to get "ui_main_ui.h" resolved

    #include "main_ui.h"
    #include "ui_main_ui.h"


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

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

  • main_ui.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// Created by Administrator on 2024/8/16 0016.
//

#ifndef CPP_SOCKET_SERVE_MAIN_UI_H
#define CPP_SOCKET_SERVE_MAIN_UI_H

#include <QMainWindow>


QT_BEGIN_NAMESPACE
namespace Ui { class main_ui; }
QT_END_NAMESPACE

class main_ui : public QMainWindow {
//Q_OBJECT 如果注释会报错

public:
explicit main_ui(QWidget *parent = nullptr);

~main_ui() override;

private:
Ui::main_ui *ui;
};


#endif //CPP_SOCKET_SERVE_MAIN_UI_H

  • main_ui.ui (qt ui)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>main_ui</class>
<widget class="QMainWindow" name="main_ui">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>main_ui</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>17</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<pixmapfunction/>
<connections/>
</ui>
  • uic generate code (ui_main_ui.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/********************************************************************************
** Form generated from reading UI file 'main_ui.ui'
**
** Created by: Qt User Interface Compiler version 6.7.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MAIN_UI_H
#define UI_MAIN_UI_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_main_ui
{
public:
QWidget *centralwidget;
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *main_ui)
{
if (main_ui->objectName().isEmpty())
main_ui->setObjectName("main_ui");
main_ui->resize(400, 300);
centralwidget = new QWidget(main_ui);
centralwidget->setObjectName("centralwidget");
main_ui->setCentralWidget(centralwidget);
menubar = new QMenuBar(main_ui);
menubar->setObjectName("menubar");
menubar->setGeometry(QRect(0, 0, 400, 17));
main_ui->setMenuBar(menubar);
statusbar = new QStatusBar(main_ui);
statusbar->setObjectName("statusbar");
main_ui->setStatusBar(statusbar);

retranslateUi(main_ui);

QMetaObject::connectSlotsByName(main_ui);
} // setupUi

void retranslateUi(QMainWindow *main_ui)
{
main_ui->setWindowTitle(QCoreApplication::translate("main_ui", "main_ui", nullptr));
} // retranslateUi

};

namespace Ui {
class main_ui: public Ui_main_ui {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAIN_UI_H

Prev:
使用python实现简易截图功能
Next:
OpenCV build and install on ubuntu