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

Cannot import QML module from resource file



  • 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


  • Moderators

    @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:/"));
    


  • 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.


Log in to reply