How to configure CLion IDE for Qt Framework?

后端 未结 7 1451
忘掉有多难
忘掉有多难 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:18

    This approach is one of the simplest way to be used for the newest Qt version.

    Qt:    5.10.1
    CLion: 2018.1.2
    GDB:   8.1
    

    Project setup

    In CLion:

    1. Create a C++ Executable/Library project
    2. Use this sample "CMakeLists.txt" for common console/gui projects that uses: QtCore, QtWidgets and QtQuick

    CMakeLists.txt:

    cmake_minimum_required(VERSION 3.10)
    project(PROJECT_NAME)
    
    set(CMAKE_CXX_STANDARD 14)
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_PREFIX_PATH "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/cmake")
    
    find_package(Qt5Core REQUIRED)
    find_package(Qt5Widgets REQUIRED)
    find_package(Qt5Quick REQUIRED)
    
    add_executable(PROJECT_NAME main.cpp MainWindow.cpp MainWindow.h qml.qrc)
    
    target_link_libraries(PROJECT_NAME Qt5::Core)
    target_link_libraries(PROJECT_NAME Qt5::Widgets)
    target_link_libraries(PROJECT_NAME Qt5::Quick)
    
    • Resource files (.qrc) should be added to add_executable list, in order for moc to be able to run its procedure on the resource and the internal file like qmls, texts, ... be accessible.

    • qml files should be included in a qrc file and load using QQmlApplicationEngine at runtime

    Debuger:

    In order to has a human-readable view in debug sessions from Qt types, A new GDB must be installed on the system and pretty printers must be available:

    Pretty printers for Qt5:

    1- Lekensteyn Qt5 Pretty Printers (Working):

    Address: https://github.com/Lekensteyn/qt5printers

    Setup: ~/.gdbinit

    python
    import sys, os.path
    sys.path.insert(0, os.path.expanduser('~/.gdb'))
    import qt5printers
    qt5printers.register_printers(gdb.current_objfile())
    end
    

    2- Official (not working!!!):

    "PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/share/qtcreator/debugger/"
    

    Setup: ~/.gdbinit

    As stated in included readme file (but not working!):

    python sys.path.insert(1, '<path/to/qtcreator>/share/qtcreator/debugger/')
    python from gdbbridge import *
    

    External tools

    In "File -> Settings -> Tools -> External Tools", add 4 external tools:

    Qt Creator:

    Program:   "PATH_TO_QT/QT_VERSION/QT_ARCH/Tools/QtCreator/bin/qtcreator"
    Arguments: $FilePath$
    

    UI Designer:

    Program:   "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/designer")
    Arguments: $FilePath$
    

    LUpdate:

    Program:   "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/lupdate")
    Arguments: $FilePath$ -ts $FileNameWithoutExtension$.ts
    

    Linguist:

    Program:   "PATH_TO_QT/QT_VERSION/QT_ARCH/lib/bin/linguist")
    Arguments: $FilePath$
    

    Now you can right click these file types and under the external tool:

    1. For .ui select Qt Creator/Designer and start UI designing
    2. For .qml select Qt Creator and design UI in QML editor
    3. For .qrc select Qt Creator and use resource editor
    4. For .cpp/.ui select LUpdate to create its translation file
    5. For .ts select Linguist and start the translating

    Automatic Beautifier

    If in Qt Creator you used a beautifier like "Uncrustify" to auto beautify the code on saving a source file, then:

    1. Install the plugin "Save Actions"
    2. Under the "File -> Settings -> Save Actions"
    3. Check:
      1. Active save actions on save
      2. Reformat file
    4. For best performance un-check:
      1. Organize imports
    0 讨论(0)
提交回复
热议问题