How to configure CLion IDE for Qt Framework?

后端 未结 7 1477
忘掉有多难
忘掉有多难 2020-12-12 12:05

How to configure CLion IDE for Qt Framework? Is this IDE compatible with Qt, or are there other IDEs compatible with Qt?

I just want to try to use something else tha

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 12:06

    I was as desperate as you, until I read this Quora discussion. It worked perfectly for me!

    To summarize, there are 2 main steps:

    Firstly, CLion uses CMake to compile your code. It is based on CMake configuration files (e.g "CMakeLists.txt"). You have to add Qt based CMake commands (the lines with 'find_package' and 'target_link_libraries'):

    cmake_minimum_required(VERSION 3.5)
    project(myqtproject)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    set(SOURCE_FILES main.cpp)
    find_package(Qt5Widgets REQUIRED)                 <-- this line
    
    add_executable(myqtproject ${SOURCE_FILES})
    
    target_link_libraries(myqtproject Qt5::Widgets)   <-- this line
    

    Secondly, CLion has to use the cmake binary installed by Qt. For that, go to: 'Preferences' -> 'Build, Execution, Deployment' -> 'CMake' and in 'CMake options' append the CMake path that Qt uses, which should be in the directory where Qt is installed. For instance, on OSX:

    -DCMAKE_PREFIX_PATH=/Users/edouard/Qt/5.7/clang_64/lib/cmake
    

    You can test that everything is working fine, by doing a little test script in main.cpp:

    #include 
    #include 
    
    using namespace std;
    
    int main() {
        qDebug() << QT_VERSION_STR;
        return 1;
    }
    

    Which should display something like:

    /Users/edouard/Library/Caches/CLion2016.2/cmake/generated/myqtproject-89a4132/89a4132/Debug/untitled
    5.7.0
    
    Process finished with exit code 1
    

    UPDATE

    I was stuck with the problem of adding Qt5 modules (for instance QSql). You can do this by adding in the CMakeLists.txt:

    find_package(Qt5Sql REQUIRED)
    

    just after the other find_package, and adding in the last line:

    target_link_libraries(myqtproject Qt5::Widgets Qt5::Sql)
    

    You can do this with all the other Qt5 modules.

提交回复
热议问题