使用clion工具编写cpp qt demo程序 qt install (windows 平台)
1 https://doc.qt.io/qt-5/windows.html
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
通过uic创建ui_file.h头文件, 配置如图
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 #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 #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 {public: explicit main_ui (QWidget *parent = nullptr) ; ~main_ui() override; private: Ui::main_ui *ui; }; #endif
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 #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); } void retranslateUi (QMainWindow *main_ui) { main_ui->setWindowTitle(QCoreApplication::translate("main_ui" , "main_ui" , nullptr)); } }; namespace Ui { class main_ui : public Ui_main_ui {}; } QT_END_NAMESPACE #endif