解决办法参考:https://stackoverflow.com/questions/35352930/qt5-qml-module-is-not-installed
engine->addImportPath()
There are a bunch of things that can lead to a module not being loaded. You can check the following:
module TestModule
in qmldir, your modules directory name must be TestModule
.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.ldd
command on Linux.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.QT_DEBUG_PLUGINS=1
and QML_IMPORT_TRACE=1
environment variablesYou 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:
MyItem20.qml
MyItem21.qml
qmldir
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.