[Development] QML preprocessor

Samuel Stirtzel s.stirtzel at googlemail.com
Mon Mar 18 10:15:36 CET 2019


Am So., 17. März 2019 um 13:35 Uhr schrieb Alberto Mardegan
<mardy at users.sourceforge.net>:
>
> Hi there!
>   When developing a QML module for the greater public one wants to
> provide a source package that can be built on multiple Qt versions.
> However, QML's lack of a preprocessor makes this rather cumbersome.
>
> Suppose that my module needs to provide an element like this:
>
> ====
> import QtQuick 2.10
>
> Item {
>     ...
>     Flickable {
>         boundsMovement: Flickable.StopAtBounds
>         ...
>     }
> }
> ====
>
> If I wanted to make this available to users of Qt 5.9 and older, I'd
> need to ship another file, instead, importing QtQuick 2.9 and removing
> that "boundsMovement" property.

To avoid duplication of qml files for different Qt versions it is
possible to export the QT_VERSION to qml:

main.cpp:
engine.rootContext()->setContextProperty("QT_VERSION", QT_VERSION);

Then in qml things like this are possible:
hueValue: (QT_VERSION >= 0x050900 ? oldColor.hslHue :
rgbToHsl(oldColor.r, oldColor.g, oldColor.b).h);

Where rgbToHsl(...) is a js function fallback for the lacking
color.hslHue in Qt 5.6

Another example for the QQC2 ScrollBar.policy in 5.9:
Component.onCompleted: {
    if(QT_VERSION >= 0x050900)
    {
        this.policy = Qt.binding(function (){ return
listView.contentHeight > listView.height ? ScrollBar.AlwaysOn :
ScrollBar.AlwaysOff; });
    }
}


-- 
Regards
Samuel



More information about the Development mailing list