Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use CLion (1.2.4) for a project involving Qt Creator?

Qt Creator makes GUI creation ridiculously easy, but I would rather use CLion for the coding part, and only use Qt Creator for the GUI editing.

like image 632
HeliosPanoptes Avatar asked Mar 13 '23 03:03

HeliosPanoptes


1 Answers

Definitely possible!

I'm on a mac, so there are a few things that I do that won't work the exact same for other operating systems. They're small, simple things that a bit of google-fu should fix relatively painlessly.

Step 1: Create a Qt Creator Project

I'll be using a widgets application for this example. I'll get to other types of projects a little later.

One thing that I would highly recommend is to change the build directory (under the Projects tab on the sidebar) to a relative path instead of an absolute one, especially if this is going into GitHub or the likes. I changed it to ./QtBinDebug.

Oh, and remember to copy or otherwise save in a separate location the contents of main.cpp for later.

That's it for Qt. We should only need to use Qt Creator when editing our GUI from now on.

Step 2: Open the project up in CLion

CLion need to create a few files in order to be able to work with our project. Tell CLion to make a new project, but name the project the exact same as the one we made in Qt Creator, and point CLion at the same path. It should warn you about files already existing, just ignore it.

CLion will have very helpfully overwritten main.cpp. Put what you copied from QtCreator back in there.

Step 3: Call the right Makefile

Qt Creator will actually generate it's own makefile that will call qmake, and do a whole bunch of other magic. It's insane... I can replace my existing CMakeLists.txt with

cmake_minimum_required(VERSION 3.3)
project(WindowTest)
set(QT_BIN "${PROJECT_SOURCE_DIR}/QtBinDebug")
add_custom_target(qtMakeFile COMMAND make -C ${QT_BIN})

This will build the project perfectly fine! The only problem is that CLion only supports CMake projects right now. The above CMakeLists gets around that by telling CMake to just call make on the right makefile, but the IDE itself doesn't know how to interpret our code......

Step 4: Find out where the Qt libraries are installed

I installed Qt via Homebrew and found the libraries I needed at /usr/local/Cellar/qt5/5.5.1_2/lib/cmake. If you told Homebrew to link the installation, /usr/local/opt/qt5/lib/cmake will also work, and has the added benefit of keeping your Qt version up to date without needing to change anything!

Step 5: Linking in the Qt Libraries for CLion

If you're like me, you'd rather use CLion when it autocompletes everything and doesn't throw red text everywhere.

We need to tell CLion where to look for the libraries it's missing. For a basic Widgets project, I needed Qt5Core and Qt5Widgets. To see what you'll need for sure, check the makefile Qt Creator generated. You should find what you need near the top in the LIBS macro.

For the linking, we're going to be using the find_package(...) and target_link_libraries(...) commands. While we could hard-code everything into these two commands, I thought breaking it up a little would be a good idea.

One other small detail is the header file that Qt Creator places in the build directory. For a skeleton widgets project, it's ui_mainwindow.h. We can tell CLion where to look with an include_directories(...) command.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(WindowTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

#Base QT directory
set(QT_PATH "/usr/local/opt/qt5")
#Build output path of your QT Creator project
set(QT_BIN "${PROJECT_SOURCE_DIR}/QtBinDebug")

#Qt5 paths
#Qt5 library path
set(QT5_LIB_DIR ${QT_PATH}/lib/cmake)
#Qt5 individual libraries
set(QT5_CORE ${QT_LIB_DIR}/Qt5Core)
set(QT5_WIDGETS ${QT_LIB_DIR}/Qt5Widgets)
set(QT5_GUI ${QT_LIB_DIR}/Qt5Gui)

#Libraries to link to. The :: is for convenience
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Gui)
#Libraries required. Probably the same as above minus the '::'. find_package() will be called on these
set(QT_LIBRARIES_REQUIRED Qt5Core Qt5Widgets Qt5Gui)

#find packages
foreach(QT_LIBRARIES_REQUIRED ${QT_LIBRARIES_REQUIRED})
    find_package( ${QT_LIBRARIES_REQUIRED} REQUIRED )
endforeach()

add_custom_target(qtMakefile COMMAND make -C ${QT_BIN})

include_directories(${QT_BIN})

set(SOURCE_FILES main.cpp mainwindow.cpp mainwindow.h mainwindow.ui)

add_executable(WindowTest ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES})

Step 6: Setting up CLion to actually run the thing properly

There should be a configuration that popped up in the run configurations menu. For me, with the above CMake file, it's qtMakefile. We need to do one last thing with that target, so click "Edit configurations..."

We need to select the executable that CLion should run after building. In the section for Executable, click "Select other..." and select the widget's executable. Mine was in QtBinDebug/WindowTest.app/Contents/MacOS/WindowTest.

And that's it! Congratulations!

Downsides

Debugging doesn't work. The debugger isn't linking on a simple hello world I put into my test program. To be honest, though, I really don't want to go down that rabbit hole.

Upsides

Autocomplete out the wazoo

like image 118
HeliosPanoptes Avatar answered Mar 23 '23 11:03

HeliosPanoptes