Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML2_IMPORT_PATH, Build Environment and project sharing



  • Qt5.11.1 // QtCreator 4.12.2
    Hi guyz,

    I'm trying to create my own Qt Module containing a custom QML, build an app using this custom QML, and then share the project with other team member. I'm lost when I try to declare the QML2_IMPORT_PATh as it seems not to be taken into account by the app at run time.

    So I created my own Qt Module Custom_Elements 1.0 relying on a custom QML element Custom_Element and attached qmldir.
    I copied the files in a specific folder on my computer like c:/specific_folder
    At static time:
    In the .pro file I declare:
    QML_IMPORT_PATH = c:/specific_folder
    In the .qml file I declare:
    Import Custom_Elements 1.0
    And the static code checking is fine, there is no issue there (the text is not underline in red)

    At run time:
    In the .pro file, if I declare:
    QML2_IMPORT_PATH = c:/specific_folder
    when I execute my app, I get the error message "module not installed"
    To solve this, I can declare the variable in the Build Environment / run time Environment, this way, the application works fine and the module can be found at run time.

    But what I am trying to do, is to define a project that I can share with other team members without forcing them to add a manual declaration in the Build Environment.

    On this page:
    https://docs.huihoo.com/qt/qtcreator/4.2/creator-build-settings.html
    It is explicitly written:
    Note: The changes are stored in the local project specific .pro.user file. Therefore, they are not suitable for sharing between developers or development PCs. To share settings, incorporate them into the build system. For example, if you use qmake, make the changes in the .pro file.

    And this is exactly what I would like to do, to declare a variable in the pro file that would make the project shareable. But it just doesn't work.

    What I tried:
    Added the debug message for the importPath list display in the main.cpp file:
    qDebug() << pEngine->importPathList();
    And what I get is:
    ("D:/Documents/Projects/myApp/debug", "qrc:/qt-project.org/imports", "C:/Tools/Qt/Qt5.14.2/5.14.2/mingw73_64/qml")
    So the QML2_IMPORT_PATH path does not appear here.

    Is this a bug, or didn't I just understand how to share this variable in the .pro file itself, so that I can share it with other team members?
    Thank you for any suggestion,

    Regards,
    Bill



  • @raven-worx
    Thank you for this extended proposal.
    I would like my app to support a dynamic path to where our Qt Module will be hosted.
    Hence I created something as follows:

    in my app.pro

    CUSTOM QML LIBRARY

    #import custom qml mibrary
    DEVELOPMENT_PATH = $$PWD
    CUSTOM_QML_LIBRARY_PATH = $$DEVELOPMENT_PATH/relative/path/to/proj5106_hmi_graphic_library
    include($$CUSTOM_QML_LIBRARY_PATH/Custom_QML_Library/Custom_QML_Library.pri)

    in the cpp file creating the qml engine:
    // CUSTOM QML LIBRARY
    #include <Custom_QML_Library.h>

    And then:
    // Import CUSTOM QML LIBRARY
    import_custom_qml_libary(*m_pEngine);

    In our custom module folder:
    in the .pri file:

    Additional import path used to resolve QML modules in Qt Creator's code model

    QML_IMPORT_PATH += $$CUSTOM_QML_LIBRARY_PATH

    DEFINES += DEVELOPMENT_PATH_MACRO="\"$${DEVELOPMENT_PATH}\""
    CUSTOM_QML_LIBRARY_PATH_MACRO="\"$${CUSTOM_QML_LIBRARY_PATH}\""

    INCLUDEPATH += $$CUSTOM_QML_LIBRARY_PATH/Custom_QML_Library

    and in the Custom_QML_Library.h file:
    #pragma once
    #include <QQmlApplicationEngine>

    void import_custom_qml_libary(QQmlApplicationEngine &_qQmlApplicationEngine);
    void import_custom_qml_libary(QQmlApplicationEngine &_qQmlApplicationEngine)
    {
    // If working dir path contains the project dir path
    // Then we consider we execute from the development environment
    // use the CUSTOM_QML_LIBRARY_PATH variable
    // Else look for the CUSTOM QML Lib Pat deployed in third_parties subfolder
    // by default
    QString development_pwd = DEVELOPMENT_PATH_MACRO;
    QString pwd = QDir::currentPath();

    if (pwd.contains(development_pwd) == true)
    {
        qDebug () << "Adding Custom QML Library from " << CUSTOM_QML_LIBRARY_PATH_MACRO;
        _qQmlApplicationEngine.addImportPath(CUSTOM_QML_LIBRARY_PATH_MACRO);
    }
    else
    {
        qDebug () << "Adding Custom QML Library from " << pwd + "/third_parties";
        _qQmlApplicationEngine.addImportPath(pwd + "/third_parties");
    }
    qDebug() << _qQmlApplicationEngine.importPathList();
    

    }

    At deployment time, we shall create the /third_parties sub folder and copy our module content their.
    Seems ok for our current solution.

    Thank you all.
    I hope this can be useful for someone.
    Regards,
    Bill



  • Have you tried to add the import path in your QQmlApplicationEngine:

    QQmlApplicationEngine engine;
    engine.addImportPath(<path to your plugins>);
    


  • Hello Loic,

    Thank you for your suggestion.
    I am aware of this alternative, and I will end using it I suppose...
    What I was trying to understand with my original post was: what is wrong with the QML2_IMPORT_PATH declared in the .pro file directly?
    I'm trying to find out if there is an actual bug, or if I am just wrongly understanding the usage of this QML2_IMPORT_PATH variable declared in the .pro file itself.

    Bill


  • Moderators

    @billouparis said in QML2_IMPORT_PATH, Build Environment and project sharing:

    What I was trying to understand with my original post was: what is wrong with the QML2_IMPORT_PATH declared in the .pro file directly?

    this variable (defined in the .pro file) will never make it into your application. it is just a hint for QtCreator.
    If you create a QML projetc from template in QtCreator you most probably will have a line with a comment like the following:

    #Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =

    also this: https://doc.qt.io/qt-5/qtqml-syntax-imports.html#qml-import-path

    Maybe you are mixing up the QML2_IMPORT_PATH environment variable?



  • @raven-worx

    Thank you for your answer.
    Maybe you are mixing up the QML2_IMPORT_PATH environment variable?
    No I think I correctly understood that QML_IMPORT_PATH is useful at static time, and to not get any issue while writing the import statement in the QML files.
    QML2_IMPORT_PATH in the opposite is useful at runtime.

    I just don't get it why I need to manually declare it in the Build/Run Environment module, and why declaring it in the .pro file does not produce the same result.

    Regards,
    Bill


  • Moderators

    @billouparis said in QML2_IMPORT_PATH, Build Environment and project sharing:

    I just don't get it why I need to manually declare it in the Build/Run Environment module, and why declaring it in the .pro file does not produce the same result.

    as i said:

    this variable (defined in the .pro file) will never make it into your application. it is just a hint for QtCreator.



  • @raven-worx
    as i said:
    this variable (defined in the .pro file) will never make it into your application. it is just a hint for QtCreator.

    Hi Raven,

    I am precisely using QtCreator, and don't understand why this variable could not automatically be added to the Build Environment variable, by QtCreator that is, just as when we add a LIBS path which automatically gets added to the "path" Build Environment variable (thanks to the checkbox 'add build library search path to PATH' I assume, or maybe because of 'Add linker library search paths to run environment').
    Thank you anyway.

    Now, I still need a valid solution to add a path from the source code and distinguish whether I run the application from my development environment, or after I deployed my application.

    Regards,
    Bill


  • Moderators

    @billouparis said in QML2_IMPORT_PATH, Build Environment and project sharing:

    I am precisely using QtCreator, and don't understand why this variable could not automatically be added to the Build Environment variable, by QtCreator that is, just as when we add a LIBS path which automatically gets added to the "path" Build Environment variable (thanks to the checkbox 'add build library search path to PATH' I assume, or maybe because of 'Add linker library search paths to run environment').

    Because thats how it is. It is the purpose of this variable, nothing more...
    The other ways how to influence import paths is already described in the link i posted.

    Now, I still need a valid solution to add a path from the source code and distinguish whether I run the application from my development environment, or after I deployed my application.

    There are many ways, for example:

    in .pro file

    DEFINES += MY_DEV_ENV
    or store the paths in a define and do whatever you want with it in C++
    DEFINES += "MY_QML_IMPORT_PATH=$$QML_IMPORT_PATH"

    in C++:

    #ifdef MY_DEV_ENV // or using QT_DEBUG is already sufficent for you?
        // debug/dev stuff
    #else 
       // release/deployed stuff
    #endif
    

    Or alternatively set the QML2_IMPORT_PATH environment variable in the QtCreator project tab and add this at the beginning of main():

    if( !qEnvironmentVariableIsSet("QML2_IMPORT_PATH ") ) // if it isn't already set (e.g. QtCreator) add a default/release path
        qputenv("QML2_IMPORT_PATH", "/my/release/qml/path");
    


  • @raven-worx
    Thank you for this extended proposal.
    I would like my app to support a dynamic path to where our Qt Module will be hosted.
    Hence I created something as follows:

    in my app.pro

    CUSTOM QML LIBRARY

    #import custom qml mibrary
    DEVELOPMENT_PATH = $$PWD
    CUSTOM_QML_LIBRARY_PATH = $$DEVELOPMENT_PATH/relative/path/to/proj5106_hmi_graphic_library
    include($$CUSTOM_QML_LIBRARY_PATH/Custom_QML_Library/Custom_QML_Library.pri)

    in the cpp file creating the qml engine:
    // CUSTOM QML LIBRARY
    #include <Custom_QML_Library.h>

    And then:
    // Import CUSTOM QML LIBRARY
    import_custom_qml_libary(*m_pEngine);

    In our custom module folder:
    in the .pri file:

    Additional import path used to resolve QML modules in Qt Creator's code model

    QML_IMPORT_PATH += $$CUSTOM_QML_LIBRARY_PATH

    DEFINES += DEVELOPMENT_PATH_MACRO="\"$${DEVELOPMENT_PATH}\""
    CUSTOM_QML_LIBRARY_PATH_MACRO="\"$${CUSTOM_QML_LIBRARY_PATH}\""

    INCLUDEPATH += $$CUSTOM_QML_LIBRARY_PATH/Custom_QML_Library

    and in the Custom_QML_Library.h file:
    #pragma once
    #include <QQmlApplicationEngine>

    void import_custom_qml_libary(QQmlApplicationEngine &_qQmlApplicationEngine);
    void import_custom_qml_libary(QQmlApplicationEngine &_qQmlApplicationEngine)
    {
    // If working dir path contains the project dir path
    // Then we consider we execute from the development environment
    // use the CUSTOM_QML_LIBRARY_PATH variable
    // Else look for the CUSTOM QML Lib Pat deployed in third_parties subfolder
    // by default
    QString development_pwd = DEVELOPMENT_PATH_MACRO;
    QString pwd = QDir::currentPath();

    if (pwd.contains(development_pwd) == true)
    {
        qDebug () << "Adding Custom QML Library from " << CUSTOM_QML_LIBRARY_PATH_MACRO;
        _qQmlApplicationEngine.addImportPath(CUSTOM_QML_LIBRARY_PATH_MACRO);
    }
    else
    {
        qDebug () << "Adding Custom QML Library from " << pwd + "/third_parties";
        _qQmlApplicationEngine.addImportPath(pwd + "/third_parties");
    }
    qDebug() << _qQmlApplicationEngine.importPathList();
    

    }

    At deployment time, we shall create the /third_parties sub folder and copy our module content their.
    Seems ok for our current solution.

    Thank you all.
    I hope this can be useful for someone.
    Regards,
    Bill


Log in to reply