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.
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.
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.
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.
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......
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!
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})
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!
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.
Autocomplete out the wazoo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With