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. Feedback needed: QML is this the right way to link other QML Files?
Forum Update on Monday, May 27th 2025

Feedback needed: QML is this the right way to link other QML Files?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml c++feedbackcmake
6 Posts 3 Posters 142 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.
  • S Offline
    S Offline
    StudentScripter
    wrote last edited by StudentScripter
    #1

    So my goal was to seperate my .qml files from the Main.qml by creating a subfolder called mymodules/ which contains my other .qml files as a library. But im not sure if my CMakeLists.txt are the most optimal. Can anyone check and give me feeback? <3 Thanks

    My structure looks like this:
    37e1c0a0-c2c0-4867-bb45-d336f08de974-image.png

    mymodules CMakeLists.txt:

    cmake_minimum_required(VERSION 3.16)
    project(mymodules VERSION 1.0 LANGUAGES CXX)
    
    find_package(Qt6 REQUIRED COMPONENTS Quick)
    
    qt_add_library(mymodules STATIC)  # <--- STATIC statt SHARED/PLUGIN
    
    qt_add_qml_module(mymodules
        URI mymodules                 # entspricht: import mymodules 1.0
        VERSION 1.0
        QML_FILES
            Buttons.qml
        RESOURCE_PREFIX /            # wichtig: sonst wird es zur Laufzeit nicht gefunden
    )
    
    target_link_libraries(mymodules
        PRIVATE Qt6::Quick
    )
    

    Main CMakeLists.txt:

    cmake_minimum_required(VERSION 3.16)
    
    project(QmlLibraryTest VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    
    find_package(Qt6 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup(REQUIRES 6.8)
    
    qt_add_executable(appQmlLibraryTest
        main.cpp
    )
    
    qt_add_qml_module(appQmlLibraryTest
        URI QmlLibraryTest
        VERSION 1.0
        QML_FILES
            Main.qml
    )
    
    
    
    
    # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
    # If you are developing for iOS or macOS you should consider setting an
    # explicit, fixed bundle identifier manually though.
    set_target_properties(appQmlLibraryTest PROPERTIES
    #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlLibraryTest
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
        MACOSX_BUNDLE TRUE
        WIN32_EXECUTABLE TRUE
    )
    
    
    
    
    # Baue und installiere das Sub‑Modul mit
    add_subdirectory(mymodules)
    
    
    target_link_libraries(appQmlLibraryTest
        PRIVATE Qt6::Quick
        mymodules  # <-- statische lib (ohne "plugin")
    
    )
    
    include(GNUInstallDirs)
    install(TARGETS appQmlLibraryTest
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        QObject::connect
            &engine,
            &QQmlApplicationEngine::objectCreationFailed,
            &app,
            []() { QCoreApplication::exit(-1); },
            Qt::QueuedConnection);
        engine.loadFromModule("QmlLibraryTest", "Main");
    
        return app.exec();
    }
    
    

    @Pl45m4

    Pl45m4P 1 Reply Last reply
    0
    • S StudentScripter

      So my goal was to seperate my .qml files from the Main.qml by creating a subfolder called mymodules/ which contains my other .qml files as a library. But im not sure if my CMakeLists.txt are the most optimal. Can anyone check and give me feeback? <3 Thanks

      My structure looks like this:
      37e1c0a0-c2c0-4867-bb45-d336f08de974-image.png

      mymodules CMakeLists.txt:

      cmake_minimum_required(VERSION 3.16)
      project(mymodules VERSION 1.0 LANGUAGES CXX)
      
      find_package(Qt6 REQUIRED COMPONENTS Quick)
      
      qt_add_library(mymodules STATIC)  # <--- STATIC statt SHARED/PLUGIN
      
      qt_add_qml_module(mymodules
          URI mymodules                 # entspricht: import mymodules 1.0
          VERSION 1.0
          QML_FILES
              Buttons.qml
          RESOURCE_PREFIX /            # wichtig: sonst wird es zur Laufzeit nicht gefunden
      )
      
      target_link_libraries(mymodules
          PRIVATE Qt6::Quick
      )
      

      Main CMakeLists.txt:

      cmake_minimum_required(VERSION 3.16)
      
      project(QmlLibraryTest VERSION 0.1 LANGUAGES CXX)
      
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      
      find_package(Qt6 REQUIRED COMPONENTS Quick)
      
      qt_standard_project_setup(REQUIRES 6.8)
      
      qt_add_executable(appQmlLibraryTest
          main.cpp
      )
      
      qt_add_qml_module(appQmlLibraryTest
          URI QmlLibraryTest
          VERSION 1.0
          QML_FILES
              Main.qml
      )
      
      
      
      
      # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
      # If you are developing for iOS or macOS you should consider setting an
      # explicit, fixed bundle identifier manually though.
      set_target_properties(appQmlLibraryTest PROPERTIES
      #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlLibraryTest
          MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
          MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
          MACOSX_BUNDLE TRUE
          WIN32_EXECUTABLE TRUE
      )
      
      
      
      
      # Baue und installiere das Sub‑Modul mit
      add_subdirectory(mymodules)
      
      
      target_link_libraries(appQmlLibraryTest
          PRIVATE Qt6::Quick
          mymodules  # <-- statische lib (ohne "plugin")
      
      )
      
      include(GNUInstallDirs)
      install(TARGETS appQmlLibraryTest
          BUNDLE DESTINATION .
          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
      )
      
      

      main.cpp:

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          QObject::connect
              &engine,
              &QQmlApplicationEngine::objectCreationFailed,
              &app,
              []() { QCoreApplication::exit(-1); },
              Qt::QueuedConnection);
          engine.loadFromModule("QmlLibraryTest", "Main");
      
          return app.exec();
      }
      
      

      @Pl45m4

      Pl45m4P Online
      Pl45m4P Online
      Pl45m4
      wrote last edited by
      #2

      @StudentScripter

      why you tagged me, I'm not an expert in QML?
      But I would put all qml files in the qt resource system.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @StudentScripter

        why you tagged me, I'm not an expert in QML?
        But I would put all qml files in the qt resource system.

        S Offline
        S Offline
        StudentScripter
        wrote last edited by
        #3

        @Pl45m4 Thought you'd may have some experience regarding the cmake aspect. :D But nevermind.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Deymos
          wrote last edited by Deymos
          #4

          Hello, i'm trying to do something similar, but only with qt_add_qml_module, check out this:
          https://github.com/Deymoss/MoneyTogether/tree/master/modules/UI
          and this
          https://forum.qt.io/topic/162027/correct-way-to-make-qml-modules-qt-6.5-6.9/14
          So far, I have not come to a solution to the problem, but I think I will solve it in the next week, thanks a lot to Mike Trahearn, he pushed this question in the qt expert community and said that he has an example that is currently still in progress, so we need to wait and there will be an answer.

          S 1 Reply Last reply
          0
          • D Deymos

            Hello, i'm trying to do something similar, but only with qt_add_qml_module, check out this:
            https://github.com/Deymoss/MoneyTogether/tree/master/modules/UI
            and this
            https://forum.qt.io/topic/162027/correct-way-to-make-qml-modules-qt-6.5-6.9/14
            So far, I have not come to a solution to the problem, but I think I will solve it in the next week, thanks a lot to Mike Trahearn, he pushed this question in the qt expert community and said that he has an example that is currently still in progress, so we need to wait and there will be an answer.

            S Offline
            S Offline
            StudentScripter
            wrote last edited by
            #5

            @Deymos Well gonna have a look at that, but nevertheless my approach posted above does work without error utilizing qt_add_qml_module but im not sure if its the best approach or has any potential errors. Thats why i was asking.

            D 1 Reply Last reply
            0
            • S StudentScripter

              @Deymos Well gonna have a look at that, but nevertheless my approach posted above does work without error utilizing qt_add_qml_module but im not sure if its the best approach or has any potential errors. Thats why i was asking.

              D Offline
              D Offline
              Deymos
              wrote last edited by
              #6

              @StudentScripter
              Yes, this approach that you provided will work without problems, but I want to try to do it without

              qt_add_library(mymodules STATIC)
              

              so that there is not a lot of junk in the project tree, and it turns out to be possible. But we have to wait

              1 Reply Last reply
              0

              • Login

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