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. Cannot import QML module from resource file

Cannot import QML module from resource file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.8k 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.
  • Q Offline
    Q Offline
    QJeroen
    wrote on 26 Feb 2018, 09:38 last edited by QJeroen
    #1

    I have the following directory layout in qrc file:

    Main.qml
    components/MenuButton.qml
    components/qmldir

    qmldir:

    import Components 1.0
    MenuButton 1.0 MenuButton.qml
    

    in main.cpp:

    engine.addImportPath(QStringLiteral("qrc:///"));
    engine.load(QUrl{QStringLiteral("qrc:///Main.qml")});
    

    Now when I import in Main.qml it doesn't work from resource file, but it does work when using local file system:

    import Components 1.0
    

    If I change the code this way:

    import "components"
    

    Than it also works from resource file. But I don't understand why I cannot load it as a module when it is placed in a resource file. It works on local file system but not in resource file. I thought it was the import path, but that seems to be correct IMHO.

    Does anybody know how to import modules from a resource file? I'm using Qt 5.10.1

    R 1 Reply Last reply 26 Feb 2018, 12:28
    1
    • Q QJeroen
      26 Feb 2018, 09:38

      I have the following directory layout in qrc file:

      Main.qml
      components/MenuButton.qml
      components/qmldir

      qmldir:

      import Components 1.0
      MenuButton 1.0 MenuButton.qml
      

      in main.cpp:

      engine.addImportPath(QStringLiteral("qrc:///"));
      engine.load(QUrl{QStringLiteral("qrc:///Main.qml")});
      

      Now when I import in Main.qml it doesn't work from resource file, but it does work when using local file system:

      import Components 1.0
      

      If I change the code this way:

      import "components"
      

      Than it also works from resource file. But I don't understand why I cannot load it as a module when it is placed in a resource file. It works on local file system but not in resource file. I thought it was the import path, but that seems to be correct IMHO.

      Does anybody know how to import modules from a resource file? I'm using Qt 5.10.1

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 26 Feb 2018, 12:28 last edited by raven-worx
      #2

      @QJeroen said in Cannot import QML module from resource file:

      engine.addImportPath(QStringLiteral("qrc:///"));

      try the following (untested):

      engine.addImportPath(QStringLiteral(":/"));
      //or
      engine.addImportPath(QStringLiteral("qrc:/"));
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QJeroen
        wrote on 5 Mar 2018, 11:01 last edited by
        #3

        The problem was that the "components" subdirectory is written in lowercase, but module import uses Components. They need to match (import statement, module line in qmldir and the directory name).

        The ModuleIdentifier is the (dotted URI notation) identifier for the module, which must match the module's install path. And the install path is relative to an import path.

        1 Reply Last reply
        1

        1/3

        26 Feb 2018, 09:38

        • Login

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