[Interest] Support for Qt 5 & Qt 6 in QML app

Vladimir b800xy at gmail.com
Fri May 13 07:51:17 CEST 2022


Wrong solution.
You need to select QML files for other versions of Qt shared library at
run-time, not compile time!
So, your executable must contain all supported versions simultaneously.
An example of my solution below.

qml.qrc
----------
<file alias="RegexValidator.qml">qml/RegexValidator.qml</file>
<file alias="+qt6/RegexValidator.qml">qml/+qt6/RegexValidator.qml</file>

main.cpp
------------
    QQmlApplicationEngine engine;
    QVersionNumber qt_ver = QLibraryInfo::version(); // Qt run-time version
    if (qt_ver.majorVersion() > 5) {
        QStringList efs(QString("qt%1").arg(qt_ver.majorVersion()));
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QQmlFileSelector::get(&engine)->setExtraSelectors(efs);
#else
        engine.setExtraFileSelectors(efs);
#endif
    }

qml/RegExpValidator.qml
----------------------------------
import QtQuick 2.12
RegExpValidator {
    id: control
    property alias regex: control.regExp
}

qml/+qt6/RegExpValidator.qml
----------------------------------------
import QtQuick.Controls 2.14
RegularExpressionValidator {
    id: control
    property alias regex: control.regularExpression
}


чт, 12 мая 2022 г. в 23:59, Alexander Dyagilev <alervdvcw at gmail.com>:

> Hello,
>
> I've just created a separate qt5compat folder inside of directory with
> my QML code.
>
> Let's suppose I need to use RectangularGlow component. For this I've
> created 3 files:
>
> 1. qt5compat/RectangularGlow.qml with the following contents:
>
> import QtQuick 2.0
> import "./qt5"
> import "./qt6"
>
> RectangularGlow
> {
>
> }
>
> 2. qt5compat/qt5/RectangularGlow.qml with the following contents:
>
> import QtQuick 2.0
> import QtGraphicalEffects 1.0
>
> RectangularGlow
> {
>
> }
>
> This file should be included into resource only when compiling using Qt5.
>
> 3. qt5compat/qt6/RectangularGlow.qml with the following contents:
>
> import QtQuick 2.0
> import Qt5Compat.GraphicalEffects
>
> RectangularGlow
> {
>
> }
>
> This file should be included into resource only when compiling using Qt6.
>
> Now, all I need to use RectangularGlow in my QML code is to import my
> custom component:
>
> import "./qt5compat"
>
> How to conditionally include resources: I've just created 2 separate
> files for this my simple qt5compat "library": qmlqt5compat_qt5.qrc and
> qmlqt5compat_qt6.qrc. Then, in my .pro file:
>
> greaterThan(QT_MAJOR_VERSION, 5):RESOURCES += qmlqt5compat_qt6.qrc
> lessThan(QT_MAJOR_VERSION, 6):RESOURCES += qmlqt5compat_qt5.qrc
>
> Hope this helps :)
>
>
> On 4/25/2022 10:41 AM, Александр Иваш wrote:
> > I need to migrate a Qt 5 application with QML ui to Qt 6 without
> > losing Qt 5 support. What is the best approach here? Version-specific
> > resources? File selectors (by the way is it even possible to select
> > QML files based on the Qt version?) I mean it is clear that
> > platform-specific code/imports needs to be wrapped into
> > platform-specific components, but what is the best way to select the
> > right component?
> > _______________________________________________
> > Interest mailing list
> > Interest at qt-project.org
> > https://lists.qt-project.org/listinfo/interest
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220513/fe1f439d/attachment.htm>


More information about the Interest mailing list