CLion develops Qt program

156 Views
1 Comment

A total of 1950 characters, expected to take 5 minutes to complete reading.

Install Qt

Download

In order to use CLion to develop Qt program, you have to have Qt Compiler it. Then let's download Qt first.

open the chinese official website of the qt:https://www.qt.io/zh-cn/

Click in the upper right corner Download and Try:
CLion develops Qt program

Click Community Users-Explore Qt Community Edition:

CLion develops Qt program

Go down and find:

CLion develops Qt program

Then download the version of your system here:

CLion develops Qt program

The above record is to prevent the website from being unable to find the download location after the revision. It is faster to download directly to the download address in the last step:https://www.qt.io/download-qt-installer-oss

install

After downloading, open the installation package to install:

CLion develops Qt program

Here you can refer to other blogger tutorials and customize the installation to install the components you want.

Installing CLion

CLion Yes C Language of IDE Compiler, very easy to use.

Download from here The latest CLion, then fool-like installation:

CLion develops Qt program

New Qt Project

We open up CLion and find the top left corner File-New-Project:

CLion develops Qt program

Find the left Qt Widget Executables:

CLion develops Qt program

Here. Qt CMake Prefix Path Select your Qt installation directory Qt directory \Qt version number \mingw_64For example, here I am. QT\6.7.0\mingw_64 You can. Then we can create the project by clicking Create in the lower right corner!

After creating a new project, we click under the project folder main.cpp:

CLion develops Qt program

I am here to prompt that the project is not configured, click Fixed-Select CMakeLists.txt You can:

CLion develops Qt program

CLion develops Qt program

If there are no other problems, we click Run in the upper right corner, which will compile successfully and display a basic window:

CLion develops Qt program

This shows that my project is ready to run!

Refactoring Project

We can find that the default new project file structure is relatively simple, all in the root directory, with the traditional C program layout is not the same.

Create the following directory under the root directory:

Directory Name Role
src Original file, such as. cpp
include A header file, such as. h
form Interface files, such as. ui .h .cpp
lib Store third-party libraries
resource Store resource files
build Compile the output file

By the way, put it again. main.cpp Put it in the src directory.

CLion develops Qt program

Then we open CMakeLists.txt, add the following statement:

CLion develops Qt program

# 搜索 src 目录
aux_source_directory(src SRC)
# 搜索 form 目录
aux_source_directory(form FORM)
# 包含 include 目录下的所有文件
include_directories(include)
# 包含 form 目录下的头文件
include_directories(form)
# 编译所有 src 和 form 目录下的文件
# 注意:这语句本来就有 只需修改为“add_executable(你的项目名称 ${SRC} ${FORM})”即可
add_executable(3DSystemHelper ${SRC} ${FORM})
# 设置编译输出的目录为 build 目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)

At this time, rebuild the project and you can find the runable files that have been generated in the build directory:

CLion develops Qt program

This is also can run oh!

Then let's try creating a new interface under the form directory:

CLion develops Qt program

CLion develops Qt program

Then modify the main.cpp file:

#include "mainwindow.h"
#include "QApplication"

int main(int argc, char *argv[]) {QApplication a(argc, argv);
    // 创建 mainwindow 对象
    mainwindow mw;
    // 显示窗口
    mw.show();
    return QApplication::exec();}

Run through:

CLion develops Qt program

Problems encountered

Cannot find libxxx.dll file

Method 1: Manually add

CLion develops Qt program

We run the compiled in the build directory. exe You may encounter this problem when you file.

CLion develops Qt program

This is because there is no use of Qt's Dynamic Link Library. We find QT directory \QT version number \mingw_64\bin directory, for example in my this is QT\6.7.0\mingw_64\bin, enter and create here. cmd window, enter:

# 加载环境
qtenv2.bat
# 动态链接库
windeployqt 你的文件.exe

CLion develops Qt program

Then many other files are generated in the build directory, and then the run becomes:

CLion develops Qt program

Method 2: Modify CMakeLists.txt

In CMakeLists.txt Add the following code, will 3DSystemHelper Modify the name of the executable generated for you:

add_custom_command(TARGET 3DSystemHelper POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Start deploy Qt..."
        COMMAND ${CMAKE_PREFIX_PATH}/bin/qtenv2.bat
        COMMAND windeployqt ${EXECUTABLE_OUTPUT_PATH}/3DSystemHelper.exe
        COMMAND ${CMAKE_COMMAND} -E echo "Deploy Qt completed!"
)

CLion develops Qt program

Then rebuild it:

CLion develops Qt program

The result is still the same, can run:

CLion develops Qt program

END
 0
Comment(1 Comment)
验证码
en_USEnglish
Article Directory