I'm trying to install a qml module that I have created into my project.
Try 1: When I register it like this:
QQmlApplicationEngine engine;
engine.addImportPath("<path>");
engine.load(...);
QQmlApplicationEngine engine;
engine.addImportPath("<path>");
engine.load(...);
To copy to clipboard, switch view to plain text mode
in my main.cpp it builds and runs.
Try 2: When I add the same path like this:
QML_IMPORT_PATH += <path>
QML2_IMPORT_PATH += <path>
QML_IMPORT_PATH += <path>
QML2_IMPORT_PATH += <path>
To copy to clipboard, switch view to plain text mode
to my .pro file, it builds and then I get an error:
QQmlApplicationEngine failed to load component
qrc:/main.qml:4 module "mymodule" is not installed
Here is a minimal example:
C:/Users/vnemkin/Documents/example/example.pro
TEMPLATE = app
QT += qml quick widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
# Try 2 : Uncomment these two, and it does not work.
#QML_IMPORT_PATH += C:/Users/vnemkin/Documents/
#QML2_IMPORT_PATH += C:/Users/vnemkin/Documents/
# Default rules for deployment.
include(deployment.pri)
TEMPLATE = app
QT += qml quick widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
# Try 2 : Uncomment these two, and it does not work.
#QML_IMPORT_PATH += C:/Users/vnemkin/Documents/
#QML2_IMPORT_PATH += C:/Users/vnemkin/Documents/
# Default rules for deployment.
include(deployment.pri)
To copy to clipboard, switch view to plain text mode
C:/Users/vnemkin/Documents/example/main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtDebug>
int main(int argc, char *argv[])
{
QQmlApplicationEngine engine;
//Try 1 : Uncomment this, it works.
//engine.addImportPath("C:/Users/vnemkin/Documents");
for(QString path
: engine.
importPathList()) {
qDebug(path.toStdString().c_str());
}
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
//Try 1 : Uncomment this, it works.
//engine.addImportPath("C:/Users/vnemkin/Documents");
for(QString path : engine.importPathList())
{
qDebug(path.toStdString().c_str());
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
C:/Users/vnemkin/Documents/example/main.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
import mymodule 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
import mymodule 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
}
To copy to clipboard, switch view to plain text mode
C:/Users/vnemkin/Documents/mymodule/qmldir
module mymodule
Example 1.0 Example.qml
module mymodule
Example 1.0 Example.qml
To copy to clipboard, switch view to plain text mode
C:/Users/vnemkin/Documents/mymodule/Example.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
Button {
}
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
Button {
}
To copy to clipboard, switch view to plain text mode
To me the Qt Documentation here seems to say both ways mentioned do the same thing:
"Additional import paths can be added through QQmlEngine::addImportPath() or the QML2_IMPORT_PATH environment variable."
http://doc.qt.io/qt-5/qtqml-syntax-imports.html
In my main.cpp I write to the debug console what engine.importPathList() contains. When I do Try 1, it contains the path, when I do Try 2, it does not.
The reason why I want to include my module in the .pro file is because there I have the $$PWD variable, and I can use that to specify the relative path from the .pro file to the module. In the .cpp code the only thing I know is where the exe is located, which depends on where we build our project.
Why is QML2_IMPORT_PATH not working for me?
I'm using:
QtCreator 3.6.1
Qt 5.6.0 (MSVC 2013, 32 bit)
Edit:
In project settings, if I go to "Run" and there "Run Environment", I have Use Build Environment specified, but it does not contain my variable.
If I add the variable:
QML2_IMPORT_PATH = C:/Users/vnemkin/Documents/
it builds and runs.
So essentially my question is: why does the build environment not contain the variables specified in my .pro file?
Bookmarks