Qt5 QML module is not installed

前端 未结 5 2033
温柔的废话
温柔的废话 2020-12-24 13:50

I\'m confused about modules in Qt QML. I\'ve read all the docs, but it doesn\'t make clear some basic ideas.

I understand that i can put a bunch of QML files into a

5条回答
  •  無奈伤痛
    2020-12-24 14:25

    I want to expand on arxarian's answer - which I think is the best technique for integrating modules - but couldn't fit those thoughts in a comment, so here's a new answer.

    It's important to first understand that qml modules and their associated resources are runtime entities and are assumed to separately exist at some location relative to the executable unless they're included in the application resources. By including them in the resources, they are still runtime entities, but they exist under the qrc:/ root path within the application binary. This hiding of the modules is the primary reason why I think this is the best technique, unless you want your modules to be open to revision after deployment, or to be independently deployable as precompiled plugins (i.e., in the resource storage of a dynamically linked library).

    So here's my explanation of the important elements of arxarian's answer:

    1. engine.addImportPath("qrc:/"); is needed in the C++ code prior to loading the qml to instruct the engine to look for qml modules in the application resource storage.
    2. RESOURCES += MyModule/mymodule.qrc is needed in the project (.pro) file to add the module's files to the application resource storage.
    3. QML_IMPORT_PATH += $$PWD in the project file tells Qt Creator to look for the module starting from the root of the project source tree. In my case I had to set this to a subdirectory (e.g., "ui/modules") because my module directories started there. The import path should include the root(s) beneath all module directories. As far as I can tell, this instruction is only for the Qt Creator IDE and does not impact the build - that's why #1 is needed.
    4. The contents of the qmldir file is as is standard for all qml modules, but it's inclusion in the module's .qrc resource file is not intuitive until you think about what's happening with runtime storage. It needs to be in the .qrc so that it is included in the application resource storage, so that the engine can find it at runtime to define the module. in the module's .qrc file defines the module subdirectory relative to the qrc:/ root in the application resource storage.
    5. The import MyModule statement in qml is now setup for success. On startup the engine will have located the module in its directory under qrc:/, parsed the qmldir file there to define the module and established the name and version from those entries.

    Hopefully this helps others understand what's going on, and please let me know if I've misunderstood anything.

提交回复
热议问题