How to configure CLion IDE for Qt Framework?

后端 未结 7 1450
忘掉有多难
忘掉有多难 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 <QApplication>
    #include <QDebug>
    
    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.

    0 讨论(0)
  • 2020-12-12 12:07

    As Tom Lank mentions, Qt projects can now be managed with, and built under CMake, which makes CLion happy.

    Qt5's CMake manual describes how. Qt provides a lot of magic under the hood here, and it's explained much better in the CMake documentation.

    One thing that isn't mentioned in the Qt CMake manual or above is that you'll also need the lines:

    set(CMAKE_AUTOUIC ON) # if you have any .ui files
    set(CMAKE_AUTORCC ON) # if you have any .qrc files 
    

    All of these calls to set() should probably come before the line find_package(Qt5Widgets REQUIRED). Also include any .ui or .qrc files as dependencies in the call to add_executable() along with your .cpp files.

    This was initially very confusing to me from browsing the web, but you shouldn't need any calls to qt_*() or qt5_*(). These have been superseded so far as I can tell.

    To test that your CMakeLists.txt actually works correctly, you can try building within Qt Creator, by loading CMakeLists.txt as a project and building.

    Once confirmed, you can load the CMakeLists.txt file as a project in CLion. Most likely, you'll need to tell CMake where to find your Qt packages with a line like this before your find_package's:

    set(CMAKE_PREFIX_PATH "C:/Qt/5.9/msvc2015_64")
    

    Finally, if you're running on / building for windows, Qt no longer comes pre-built with GCC/Mingw32 libraries. You need to build with visual studio. Luckily, CLion now supports Visual Studio experimentally and I've found it to work for Qt projects; just be sure to set the architecture (under Settings->Build, Execution, Development->CMake) to x86_amd64, in order to build in 64-bit mode and be compatible with Qt's pre-build libs.

    All of this is tested with CLion 2017.1, Qt 5.9, and the Visual Studio 2015 compiler.

    0 讨论(0)
  • 2020-12-12 12:10

    You can build QT applications in CLion. QT Provides CMake modules that take care of all details.

    The following CMake script builds the example application 'Dynamic Layouts Example'

    cmake_minimum_required(VERSION 3.7)
    project(qtlayoutexample)
    set(CMAKE_CXX_STANDARD 14)
    
    # Find QT packages
    find_package(Qt5Widgets)
    
    # Add the include directories for the Qt 5 Widgets module to
    # the compile lines.
    include_directories(${Qt5Widgets_INCLUDE_DIRS})
    
    # Find includes in corresponding build directories
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    # Instruct CMake to run moc automatically when needed.
    set(CMAKE_AUTOMOC ON)
    
    # Add compiler flags for building executables (-fPIE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
    
    qt5_generate_moc(main.cpp main.moc)
    
    # Tell CMake to create the qtlayoutexample executable
    add_executable(qtlayoutexample main.cpp dialog.cpp main.moc)
    
    #Link the qtlayoutexample executable to the Qt 5 widgets library.
    target_link_libraries(qtlayoutexample Qt5::Widgets)
    

    More information regarding building Qt applications with CMake.

    0 讨论(0)
  • 2020-12-12 12:10

    You can easily develop Qt with VC, Eclipse, CLion etc. when you use CMake as a build tool. CMake will generate the project files for each IDE. I was using several IDEs this way. After this journey I am an even happier user of Qt Creator.

    0 讨论(0)
  • 2020-12-12 12:12

    This link has a quickstart project, you just have to change your CMAKE_PREFIX_PATH in CMakeLists to the location of the Qt packaged compiler you want to use (mine is gcc_64, his by default is clang_64)-- it has some of the settings mentioned by other answers already set:

    https://github.com/ArneGockeln/qtstarter

    In addition, (on Ubuntu-based Linux) I had to install the OpenGL libraries as described here (https://askubuntu.com/questions/11378/how-do-i-set-up-an-opengl-programming-environment).

    0 讨论(0)
  • 2020-12-12 12:14

    The only thing you need is to add QT install ..Qt\5.10.1\mingw53_32\bin; to your PATH. Don't forget to restart PC afterwards, because CLion for some reason isn't able to refresh the path, only full pc restart helps.

    0 讨论(0)
提交回复
热议问题