Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qtcreator qml component directories with cmake
Forum Update on Monday, May 27th 2025

qtcreator qml component directories with cmake

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Hollensen
    wrote on 9 May 2021, 19:50 last edited by
    #1

    I am trying to convert one last project from qmake to cmake and encountering a change in how qml files are handles.
    When using qmake, custom components in a different directory are found without any import "directory" statements.
    When using cmake, the project still builds and runs fine, but within QtCreator custom components are marked in red with Unknown component. (M300). If I follow https://doc.qt.io/qt-5/qtqml-syntax-directoryimports.html and explicitly import the directory, then the editor is happy, but the build fails to run with error "no such directory".

    If possible, I would like to retain the qmake behavior of just having qrc files which I tell cmake about.

    Here is a minimal project which shows the same issue for me in both QtCreator 4.14 and now 4.15 with Qt 5.15.
    CMakeLists.txt replacing project.pro
    qml.qrc
    main.cpp
    main.qml
    components/components.qrc
    components/CustomLabel.qml

    project.pro

    QT += quick
    SOURCES += main.cpp
    RESOURCES += qml.qrc components/components.qrc
    TARGET = qml_testing
    

    CMakeLists.txt :

    cmake_minimum_required(VERSION 3.16)
    project(qml_testing LANGUAGES CXX)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTOUIC ON)
    set(CMAKE_AUTORCC ON)
    find_package(Qt5 COMPONENTS Core Gui Quick REQUIRED)
    add_executable(${PROJECT_NAME} main.cpp qml.qrc components/components.qrc)
    target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Gui Qt5::Quick)
    

    qml.qrc :

    <RCC>
        <qresource prefix="/">
            <file>main.qml</file>
        </qresource>
    </RCC>
    

    main.cpp :

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char* argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return QGuiApplication::exec();
    }
    

    main.qml :

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    //import "components"
    
    ApplicationWindow {
        id: root
        width:  360
        height: 640
        visible: true
        
        CustomLabel {
            text: "Label Text"
        }
    }
    

    components/components.qrc :

    <RCC>
        <qresource prefix="/">
            <file>CustomLabel.qml</file>
        </qresource>
    </RCC>
    

    components/CustomLabel.qml :

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    
    Label {
        id: root
        width: 220
        font.pixelSize: 13
    }
    
    1 Reply Last reply
    1
    • M Offline
      M Offline
      Mammamia
      wrote on 9 May 2021, 20:33 last edited by
      #2

      @Paul-Hollensen You might also need to set QML_IMPORT_PATH like
      set(QML_IMPORT_PATH ${PROJECT_SOURCE_DIR}/components CACHE STRING "QML Import Paths")

      Also in the main.cpp try to initialize the QRC file using Q_INIT_RESOURCE(components);

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paul Hollensen
        wrote on 9 May 2021, 21:32 last edited by
        #3

        Thanks for your reply, Mammamia.
        Neither setting QML_IMPORT_PATH nor adding Q_INIT_RESOURCE resolved the issue.
        My understanding is QML_IMPORT_PATH is for modules, which I am not building, so qmake or QtCreator must be setting something else.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mammamia
          wrote on 10 May 2021, 07:21 last edited by
          #4

          @Paul-Hollensen
          Ok, I found it.

          In your components.qrc change the prefix from "/" to "components"

          <RCC>
              <qresource prefix="/components">
                  <file>CustomLabel.qml</file>
              </qresource>
          </RCC>
          
          1 Reply Last reply
          2
          • P Offline
            P Offline
            Paul Hollensen
            wrote on 10 May 2021, 16:14 last edited by
            #5

            Thank you again, that did it, and has also given me a better sense of how this works.
            I'd still like to know if it is possible for QtCreator to find such components without an import using cmake, as it does with qmake.
            But I can move forward. Marking solved. Grazie!

            1 Reply Last reply
            0

            1/5

            9 May 2021, 19:50

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved