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. Loader: how to setSource when target qmlfile resides in qml module
QtWS25 Last Chance

Loader: how to setSource when target qmlfile resides in qml module

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 413 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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by Redman
    #1

    Hello,

    I have a Client application which imports several qml modules.

    Im working on a menu bar for my application. In total I have four buttons right now. Each button is supposed to load a different qml component.
    The qml components generally get imported via following code. I just provided MyQml module. For the other modules it should be the same.

    engine.addImportPath(QStringLiteral(":/MyQml/imports"));
    

    which works fine. When I do MyQml {} everything works fine and the component is displayed correctly.
    All the needed qml imports are present.

                MyButton {
                    id: btn1
                    backgroundColor: "white"
                    icon: "qrc:/icon/factory.svg"
                    view: "MyQml.qml"
    
                    onClicked: root.viewSelected(view)
                }
    

    This button is part of the menu bar. I omited the rest of the implementation, because I think the general flow is clear.

    Unfortunately, when given the raw string of MyQml.qml to the Loader I get following error qrc:/qt/qml/Client/qml/MyQml.qml: No such file or directory. Obviously qrc can not find the qml there, bc it resides in the module.

    How am I supposed to reference a qml-file by string that resides in a module to be able to pass it to the Loader so its gets loaded successfully?

    1 Reply Last reply
    0
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      The import path is irrelevant to the Loader. It's source has to be set to a valid QUrl.
      If I interpret it correctly, that's :/MyQml/imports/MyQml.qml in your case.

      If it's not, you can add the following to main.cpp to dump all available resource files:

      #include <QDirIterator> // at the top, unless already included.
      
      QDirIterator it(":", QDirIterator::Subdirectories);
      while (it.hasNext())
          qInfo() << it.next();
      

      Software Engineer
      The Qt Company, Oslo

      R 1 Reply Last reply
      2
      • Axel SpoerlA Axel Spoerl

        The import path is irrelevant to the Loader. It's source has to be set to a valid QUrl.
        If I interpret it correctly, that's :/MyQml/imports/MyQml.qml in your case.

        If it's not, you can add the following to main.cpp to dump all available resource files:

        #include <QDirIterator> // at the top, unless already included.
        
        QDirIterator it(":", QDirIterator::Subdirectories);
        while (it.hasNext())
            qInfo() << it.next();
        
        R Offline
        R Offline
        Redman
        wrote on last edited by Redman
        #3

        @Axel-Spoerl said in Loader: how to setSource when target qmlfile resides in qml module:

        The import path is irrelevant to the Loader. It's source has to be set to a valid QUrl.

        If I interpret it correctly, that's :/MyQml/imports/MyQml.qml in your case.

        Thank you very much!

        The final solution was to import the file as following:

        "qrc:/MyQml/imports/core/plugins/MyQml/qml/MyQml.qml"
        qrc:/RESOURCE_PREFIX/URI/MyQml.qml

        Which is concatenated from following cmake configuration:

        qt_add_qml_module(${PROJECT_NAME} STATIC
            URI core.plugins.myqml
            VERSION 1.0
            RESOURCE_PREFIX myqml/imports
            SOURCES
            ...
        )
        
        1 Reply Last reply
        0
        • R Redman has marked this topic as solved on

        • Login

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