[Interest] Clean way to define and categorize constants in QML

Viktória Nemkin viktoria.nemkin at gmail.com
Wed May 11 14:02:33 CEST 2016


Hello!

Thank you everyone for replying to this thread, I'm sorry for the late
response.

I have been fiddling with all of these for a while, and come to this
solution:

pragma Singleton

import QtQuick 2.7


QtObject {


    property QtObject font : QtObject {

        readonly property color red: "#D8423B"

        readonly property color white: "#F4F4F4"

        readonly property color purple: "#8E3D7F"

    }


    property QtObject background : QtObject {

        readonly property color red: "#D8423B"

        readonly property color white: "#F4F4F4"

        readonly property color purple: "#8E3D7F"

    }


}


Then when using it: Colors.background.red to assign.


This is basically what Dmitry Volosnykh suggested, but I had to remove
the readonly

attribute from the nested QtObjects.


If I don't remove them, like this:


pragma Singleton

import QtQuick 2.7

QtObject {

    readonly property QtObject font : QtObject {

        readonly property color red: "#D8423B"

        readonly property color white: "#F4F4F4"

        readonly property color purple: "#8E3D7F"

    }


    readonly property QtObject background : QtObject {

        readonly property color red: "#D8423B"

        readonly property color white: "#F4F4F4"

        readonly property color purple: "#8E3D7F"

    }

}


I get a few syntax errors on the line readonly property QtObject font :
QtObject

saying: Expected token 'some comma, etc...' I wonder why this happens.


But regardless, thank you. :)


Viki


On 3 May 2016 at 07:49, Sina Dogru <sinadooru at gmail.com> wrote:

>
>
> 2016-05-02 17:31 GMT+03:00 Jérôme Godbout <jerome at bodycad.com>:
>
>> Hello Sina, this will not work (at least up to 5.5 as far as I know).
>> Cause QtObject doesn't have a default property to place children like Item
>> have. You would get the following error while parsing your script:
>> *Cannot assign to non-existent default property*
>>
>> You can either make a simple object that have the default property for
>> children and property parent like Items (here's the QmlObject) or simply
>> use the Item {} and get the extra position,dimension properties.
>>
>> class QmlObject : public QObject, public QQmlParserStatus
>> {
>>         Q_OBJECT
>> Q_INTERFACES(QQmlParserStatus)
>>         Q_CLASSINFO("DefaultProperty", "children");
>>
>> Q_PROPERTY(QQmlListProperty<QObject> children READ children NOTIFY
>> childrenChanged STORED false);
>>         Q_PROPERTY(QObject* parent READ parent WRITE set_parent NOTIFY
>> parentChanged STORED false);
>>
>> Q_PROPERTY(QString idName READ id_name NOTIFY idNameChanged STORED false);
>>       ...
>> };
>>
>> Thank you so much Jérôme, I like the idea that categorizing the
> constants. I was not aware that childing the QtObjects are not possible
> because of the default property. Also thank you for showing the basic
> structure for QmlObject.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160511/d4f91c94/attachment.html>


More information about the Interest mailing list