Results 1 to 7 of 7

Thread: QQmlEngine::addImportPath(...) vs QML2_IMPORT_PATH difference?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QQmlEngine::addImportPath(...) vs QML2_IMPORT_PATH difference?

    I'm trying to install a qml module that I have created into my project.

    Try 1: When I register it like this:
    Qt Code:
    1. QQmlApplicationEngine engine;
    2. engine.addImportPath("<path>");
    3. 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:
    Qt Code:
    1. QML_IMPORT_PATH += <path>
    2. 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
    Qt Code:
    1. TEMPLATE = app
    2.  
    3. QT += qml quick widgets
    4.  
    5. CONFIG += c++11
    6.  
    7. SOURCES += main.cpp
    8.  
    9. RESOURCES += qml.qrc
    10.  
    11. # Additional import path used to resolve QML modules in Qt Creator's code model
    12. # Try 2 : Uncomment these two, and it does not work.
    13. #QML_IMPORT_PATH += C:/Users/vnemkin/Documents/
    14. #QML2_IMPORT_PATH += C:/Users/vnemkin/Documents/
    15.  
    16. # Default rules for deployment.
    17. include(deployment.pri)
    To copy to clipboard, switch view to plain text mode 


    C:/Users/vnemkin/Documents/example/main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QtDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QQmlApplicationEngine engine;
    10. //Try 1 : Uncomment this, it works.
    11. //engine.addImportPath("C:/Users/vnemkin/Documents");
    12.  
    13. for(QString path : engine.importPathList())
    14. {
    15. qDebug(path.toStdString().c_str());
    16. }
    17.  
    18. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    C:/Users/vnemkin/Documents/example/main.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3. import QtQuick.Dialogs 1.2
    4. import mymodule 1.0
    5.  
    6. ApplicationWindow {
    7. visible: true
    8. width: 640
    9. height: 480
    10. title: qsTr("Hello World")
    11. }
    To copy to clipboard, switch view to plain text mode 
    C:/Users/vnemkin/Documents/mymodule/qmldir
    Qt Code:
    1. module mymodule
    2.  
    3. Example 1.0 Example.qml
    To copy to clipboard, switch view to plain text mode 
    C:/Users/vnemkin/Documents/mymodule/Example.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3. import QtQuick.Dialogs 1.2
    4.  
    5. Button {
    6.  
    7. }
    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?
    Last edited by viktoria.nemkin; 14th April 2016 at 13:36.

Similar Threads

  1. What is the difference between (.) and (->) on this code
    By rezas1000 in forum General Programming
    Replies: 2
    Last Post: 30th August 2014, 16:02
  2. The difference between tr and translate
    By rezas1000 in forum Newbie
    Replies: 1
    Last Post: 15th August 2014, 12:29
  3. Difference between ' & "
    By Atomic_Sheep in forum General Programming
    Replies: 2
    Last Post: 24th January 2013, 13:06
  4. What's the difference?
    By Atomic_Sheep in forum Newbie
    Replies: 3
    Last Post: 6th September 2012, 11:36
  5. difference between two objects
    By rk0747 in forum Newbie
    Replies: 1
    Last Post: 15th April 2010, 09:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.