Qt5 QML module is not installed

解决办法参考:https://stackoverflow.com/questions/35352930/qt5-qml-module-is-not-installed

  • I think installed means they are located in the proper paths, so
    that they can be found at runtime
  • You should not necessarily create/build a QmlExtrensionPlugin for that purpose. You can also use as a module plain QML files in one directory with a qmldir describing this module. It's the matter of distributing your code - with QmlExtensionPlugin you provide the module compiled, if you want to hide the code.
  • The modules can be in resources system or as files on disk, it's up to you.
  • The app looks for modules in predefined paths - in your app's directory, in QT plugins path, in QML2_IMPORT_PATH, in directories that you added using engine->addImportPath()

There are a bunch of things that can lead to a module not being loaded. You can check the following:

  1. Module identifier in qmldir should be the same as the directory name, where the module actually resides. For example if your module has module identifier module TestModule in qmldir, your modules directory name must be TestModule.
  2. If it is a QML extension plugin (shared library), make sure that plugin's directory name is the same as plugin's name.
  3. export QML2_IMPORT_PATH (make sure there is 2 in the name) env variable to point to directory containing your module. There is also a QQmlEngine::addImportPath method, which adds the directory to the list to lookup for plugins.
  4. If it is a qml extension plugin (shared library), then there might be missing dependencies for it. You can check it by Dependency Walker on Windows or ldd command on Linux.
  5. Setting QT_PLUGIN_PATH runtime variable may help to load plugins. It should point to a directory containing you plugin's directory, not the plugin's directory itself.
  6. You can also enable traces to see what's going on while plugins are loaded for better understanding of the problem - export QT_DEBUG_PLUGINS=1 and QML_IMPORT_TRACE=1environment variables

You can also read this link: https://doc.qt.io/qt-5/qtqml-modules-identifiedmodules.html

In my case (I have all QML files in qrc resources) worked to add qmldir to resources also and call method addImportPath("qrc:/") of QQmlApplicationEngine.

My main.cpp looks like:

QQmlApplicationEngine engine;
engine.addImportPath("qrc:/");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

Important parts of my .pro file looks like:

RESOURCES += qml.qrc \
    MyModule/mymodule.qrc

QML_IMPORT_PATH += $$PWD

My qmldir:

module MyModule

MyItem          2.0 MyItem20.qml
MyItem          2.1 MyItem21.qml

My qrc:

<RCC>
    <qresource prefix="/MyModule">
        <file>MyItem20.qml</file>
        <file>MyItem21.qml</file>
        <file>qmldir</file>
    </qresource>
</RCC>

And finally my main.qml:

import QtQuick 2.5
import QtQuick.Window 2.2

import MyModule 2.0

Window {
    visible: true
    width: 640
    height: 480

    MyItem {
        anchors.fill: parent
    }
}

QtCreator is happy (not underlining components and imports) and module is loaded. Hope this helps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值