Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML plugins are not imported when using CMake and static Qt



  • Hello!

    Recently I have been struggling with CMake & static Qt build.
    Consider a very simple application written with QML:

    import QtQuick 2.14
    import QtQuick.Controls 2.14
    import QtQuick.Window 2.14
    
    Window {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
    
      Button {
        anchors.centerIn: parent
        text: "Button :)"
      }
    }
    

    When I build this application using qmake everything works fine. I can run my app without any troubles, everything is packed inside executable. However, using CMake for the same project produces executable that not contain qml plugins (Error in runtime: QQmlApplicationEngine failed to load component qrc:/main.qml:1:1: module "QtQuick" plugin "qtquick2plugin" not found

    My CMake file:

    cmake_minimum_required(VERSION 3.5)
    
    project(cmake_sample LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(BUILD_SHARED_LIBS OFF)
    
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    
    find_package(Qt5 COMPONENTS Core Quick REQUIRED)
    
    add_executable(cmake_sample
        main.cpp
        qml.qrc
    )
    
    target_compile_definitions(cmake_sample
      PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
    target_link_libraries(cmake_sample
      PRIVATE Qt5::Core Qt5::Quick)
    

    To fix this I have to manually import & link all required QML plugins. While it solves the problem, it's very bulky on larger projects. I have the impression that these plugins should be automatically imported (others, like imageformats are ...)

    Question: Is it intended in CMake or it is a bug? If it is intended, is there any correct and elegant solution to properly configure CMake project against static Qt?



  • Had the same issue and got the hint that you can do:

    find_package(Qt5QmlImportScanner REQUIRED)
    qt5_import_qml_plugins(yourapp)
    



Log in to reply