Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Qt on CMake Workshop Summary – May ’19

Porting Qt to the CMake build system

Excerpt of the top-level CMakeLists.txt in qtbase.git

### Usecase: application wants to mix both Qt5 and Qt6, to allow gradual porting 

set(QT_CREATE_VERSIONLESS_TARGETS OFF)
find_package(Qt5 COMPONENTS Core Gui Widgets) # Creates only Qt5::Core
find_package(Qt6 COMPONENTS Core Gui Widgets) # Creates only Qt6::Core
target_link_libraries(myapp1 Qt5::Core)
target_link_libraries(myapp2 Qt6::Core)

### Usecase: application doesn't mix Qt5 and Qt6, but allows to fully switch to link against either Qt5 or Qt6 

set(MY_APP_QT_MAJOR_VERSION 6) # <- potentially set at command line by application developer
# set(QT_CREATE_VERSIONLESS_TARGETS ON) <- Default, doesn't need to be set
find_package(Qt${MY_APP_QT_MAJOR_VERSION} COMPONENTS Core Gui Widgets) # Creates Qt5::Core and Qt::Core OR Qt6::Core and Qt::Core, based on the variable
target_link_libraries(myapp Qt::Core) # Just links to whatever Qt major version was requested