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

version number for QML imports



  • In QML code, is it recommended to specify the lowest import version number you can, or the highest import version you can?

    I have tended to use the lowest import version number I can, so I keep at the most minimum QML "API" for the features I need, and also so my QML could work on older Linux systems that have an older version of Qt 5 libraries.

    I wonder if that approach can cause any drawbacks on a more up-to-date system that has more up-to-date Qt libraries - for example, could I be missing code optimizations, fixes, etc.

    I guess I am wondering about any tradeoffs between "import version number" and "optimal code execution".

    Perhaps the recommendation will be to use the QML Profiler and see if there is any difference, and pick the one that best suits the situation.

    Still, I wonder if there is a general recommendation.


  • Moderators

    @VStevenP said in version number for QML imports:

    In QML code, is it recommended to specify the lowest import version number you can, or the highest import version you can?

    Lowest. This gives you most flexibility, while not losing any optimisations and features.

    But in truth - it does not matter. Internally, QML engine always loads the newest code it has. There even was a plan to completely drop module versioning in Qt 6, but it didn't happen (yet).

    I have tended to use the lowest import version number I can, so I keep at the most minimum QML "API" for the features I need, and also so my QML could work on older Linux systems that have an older version of Qt 5 libraries.

    I wonder if that approach can cause any drawbacks on a more up-to-date system that has more up-to-date Qt libraries - for example, could I be missing code optimizations, fixes, etc.

    No, you're not missing anything this way, and you leave a "door open" to lower Qt version if you need to, without having to manually change all import statements in all QML files.

    I guess I am wondering about any tradeoffs between "import version number" and "optimal code execution".

    Perhaps the recommendation will be to use the QML Profiler and see if there is any difference, and pick the one that best suits the situation.

    Still, I wonder if there is a general recommendation.

    General recommendation is "it does not matter, we ignore version numbers anyway" ;)



  • @sierdzio

    But it can affect properties available in objects:

    import QtQuick 2.14
    
    Image {
      sourceClipRect: Qt.rect(0,0,100,100)
    }
    

    Result:

    QQmlApplicationEngine failed to load component
    qrc:/main.qml:352:9: "Image.sourceClipRect" is not available in QtQuick 2.14.
    

    Once I increase that value to 2.15 it works fine.

    Are you talking about something else?



  • Based on all the statements he made, I think he's saying the imports just control the API (what properties are available), but that beyond that, it is always using the latest code. So, it's really just an API control.

    Therefore, it makes sense to use the lowest numbered import statements if you wish to limit the API in your code, in the case your code needs to run on an older version of Qt and you wish to ensure it will not use any features unavailable in that version.

    Thanks for your answers. I am going to mark this Resolved.


  • Moderators

    @fcarney said in version number for QML imports:

    But it can affect properties available in objects:

    Yes, and OP is aware of it:

    I have tended to use the lowest import version number I can, so I keep at the most minimum QML "API" for the features I need

    So in my answer I focused on other stuff.

    Of course, you should not lower the number so much that you start loosing features you need :-)


Log in to reply