QML import compatible with both Qt5 and Qt6
-
Hi,
I want my QML code to run on both Qt 5.15 and Qt 6.
Qt5: import QtQuick.Controls 2.15
Qt6: import QtQuick.Controls
Is there a way to write a single import statement that works on both, similar to how we do version checks in C++:
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
// Qt6 code
#else
// Qt5 code
#endifThanks
-
Including a version number in the import statement is still valid syntax in Qt 6.
https://doc.qt.io/qt-6/qtqml-syntax-imports.html#module-namespace-imports
-
https://doc.qt.io/qt-6/qtqml-syntax-imports.html#module-namespace-imports
it says
An example of an unqualified import with version would beimport QtQuick 2.10
In that case, any types defined in QtQuick 2.11 and higher or in any higher major version, like 6.0, would not be available to the file.
-
https://doc.qt.io/qt-6/qtqml-syntax-imports.html#module-namespace-imports
it says
An example of an unqualified import with version would beimport QtQuick 2.10
In that case, any types defined in QtQuick 2.11 and higher or in any higher major version, like 6.0, would not be available to the file.
@Likhitha_s said in QML import compatible with both Qt5 and Qt6:
https://doc.qt.io/qt-6/qtqml-syntax-imports.html#module-namespace-imports
it says
An example of an unqualified import with version would be [...]Is there a question about this section of the documentation? I don't understand the purpose in repeating it without comment.
-
https://doc.qt.io/qt-6/qtqml-syntax-imports.html#module-namespace-imports
it says
An example of an unqualified import with version would beimport QtQuick 2.10
In that case, any types defined in QtQuick 2.11 and higher or in any higher major version, like 6.0, would not be available to the file.
@Likhitha_s Hi,
This comment just states that if you use versioned imports you will stay to that exact version.
Since you want your code to work with both Qt 5 and Qt 6, it's in fact exactly what you want.
-
Hi,
I want my QML code to run on both Qt 5.15 and Qt 6.
Qt5: import QtQuick.Controls 2.15
Qt6: import QtQuick.Controls
Is there a way to write a single import statement that works on both, similar to how we do version checks in C++:
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
// Qt6 code
#else
// Qt5 code
#endifThanks
@Likhitha_s said in QML import compatible with both Qt5 and Qt6:
Qt5: import QtQuick.Controls 2.15
Qt6: import QtQuick.Controls
You can use
import QtQuick.Controls 2.15for both Qt 5 and Qt 6.