Results 1 to 7 of 7

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

  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 14:36.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by viktoria.nemkin View Post
    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."
    Did you set that environment variable somewhere?

    Quote Originally Posted by viktoria.nemkin View Post
    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.
    Isn't the module built in a relative path?
    Where to you plan to have it when the application is deployed/installed?

    Quote Originally Posted by viktoria.nemkin View Post
    Why is QML2_IMPORT_PATH not working for me?
    Have you actually set it in the environment you are launching the application from?

    Quote Originally Posted by viktoria.nemkin View Post
    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.
    As it should.

    So I take it you hadn't had set the variable before.


    Quote Originally Posted by viktoria.nemkin View Post
    So essentially my question is: why does the build environment not contain the variables specified in my .pro file?
    Not sure what you mean.

    Your code snippets suggest that you have a qmake variable with the same name in your .pro file, do you have the actual environment variable set in the environment the build runs in?
    I.e. did you run QtCreator in an environment with the variable set?

    Cheers,
    _

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

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

    I did not run Qt with this environment variable set. So now I understand that the variables in the .pro file are not the same as build/run environment variables.

    I want to have all the qmls inside the module to be built into the application/not deploy them separately. After reading your answer I think I should not be using a module for this. (?)

    My original problem that made me start to use modules was that I have many projects in a folder, just like the example project I included in my question and I have a folder next to them just like mymodule which contains qmls used by all projects (common qml components I made).

    However, when I try to include this folder in example/main.qml, like this:

    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. import "../mymodule"
    6.  
    7. ApplicationWindow {
    8. visible: true
    9. width: 640
    10. height: 480
    11. title: qsTr("Hello World")
    12.  
    13. Example{
    14.  
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    In the code snippet Example is purple, which means the editor can find it in mymodule. But when I build it, it says "../mymodule" is not a directory.

    If I copy paste the mymodule folder inside the project folder (to example/mymodule) and import "mymodule" it finds it and runs.

    Also if I put import ".." it says ".." is not a directory.

    Why can't I import anything above the project's folder?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by viktoria.nemkin View Post
    I did not run Qt with this environment variable set. So now I understand that the variables in the .pro file are not the same as build/run environment variables.
    Right.

    Quote Originally Posted by viktoria.nemkin View Post
    I want to have all the qmls inside the module to be built into the application/not deploy them separately. After reading your answer I think I should not be using a module for this. (?)
    You could set the path during development to the local path you know and when you're done you add your module's files to a Qt resource and set the import path to include the resource path.

    Or you just use the "relative path" syntax for import, which will get the QML files from the resource if the main QML file came from the resource or from the file system, if the main file was loaded from a local file.

    Quote Originally Posted by viktoria.nemkin View Post
    In the code snippet Example is purple, which means the editor can find it in mymodule. But when I build it, it says "../mymodule" is not a directory.
    That is a relative path "one directory up, then subdirectory mymodule", applied to the path of the current QML file.
    So if your main.qml is example/main.qml, then "mymodule" and "example" need to be sibling directories.

    Quote Originally Posted by viktoria.nemkin View Post
    Why can't I import anything above the project's folder?
    I don't think there is any such restriction, check if you have the right relative path.

    Cheers,
    _

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

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

    That is a relative path "one directory up, then subdirectory mymodule", applied to the path of the current QML file.
    So if your main.qml is example/main.qml, then "mymodule" and "example" need to be sibling directories.
    But they are. If you look at the example I included you can see the hierarchy (above all code snippets I put the absolute path to the file).

    The only thing I modify in it is example/main.qml, where I remove the line

    import mymodule 1.0

    and add

    import "../mymodule"

    So I'm trying to import it as a simple directory, not a qmldir now.

    Then I build it like this I get an error message saying qrc:/main.qml:4 "../mymodule": no such directory. But it is there.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by viktoria.nemkin View Post
    But they are. If you look at the example I included you can see the hierarchy (above all code snippets I put the absolute path to the file).
    Yes, but you are not loading the files from disk.

    Quote Originally Posted by viktoria.nemkin View Post
    Then I build it like this I get an error message saying qrc:/main.qml:4 "../mymodule": no such directory. But it is there.
    You are loading main.qml from the resource system.
    Try to apply "../mymodule" to that path and you end up with an invalid URL "qrc:/../mymodule"

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    viktoria.nemkin (15th April 2016)

  8. #7
    Join Date
    Apr 2016
    Posts
    9
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Thank you! Now I understand it.

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, 17:02
  2. The difference between tr and translate
    By rezas1000 in forum Newbie
    Replies: 1
    Last Post: 15th August 2014, 13:29
  3. Difference between ' & "
    By Atomic_Sheep in forum General Programming
    Replies: 2
    Last Post: 24th January 2013, 14:06
  4. What's the difference?
    By Atomic_Sheep in forum Newbie
    Replies: 3
    Last Post: 6th September 2012, 12:36
  5. difference between two objects
    By rk0747 in forum Newbie
    Replies: 1
    Last Post: 15th April 2010, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.