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

Jérôme Godbout jerome at bodycad.com
Fri Apr 29 20:36:42 CEST 2016


Note, you can also have to add the QObject* parent on the QmlObject.

On Fri, Apr 29, 2016 at 2:34 PM, Jérôme Godbout <jerome at bodycad.com> wrote:

>
>    1. You can do an Item as root object to put children. Annoying because
>    it get a lot of unused properties.
>    2. Or you can do your own QtObjectWithChildren from C++, which is just
>    a QObject with children default property list of QObject*. (We did this and
>    call them QmlObject)
>    3. Or use a javascript map, annoying to emit changed() when modifying
>    a single value inside the map.
>
> ex 2 implemented:
> QmlObject
> {
>   readonly property alias myCategory: myCategory_
>
>   QmlObject
>   {
>      id: myCategory_
>      readonly property color red: "#FF00000"
>   }
> }
>
> On Fri, Apr 29, 2016 at 2:12 PM, Viktória Nemkin <
> viktoria.nemkin at gmail.com> wrote:
>
>> Thank you for your help. The syntax for creating a named object property
>> is what I needed.
>>
>> Regards,
>> Viki
>>
>> On 29 April 2016 at 13:12, Kristoffersen, Even (NO14) <
>> Even.Kristoffersen at honeywell.com> wrote:
>>
>>> Those internal elements are not directly accessible.
>>>
>>> You can try exposing them with the use of *alias* in the root object.
>>>
>>>
>>>
>>>
>>>
>>> -Even
>>>
>>>
>>>
>>> *From:* Interest [mailto:interest-bounces+even.kristoffersen=
>>> honeywell.com at qt-project.org] *On Behalf Of *Viktória Nemkin
>>> *Sent:* 29. april 2016 13:04
>>> *To:* interest at qt-project.org
>>> *Subject:* [Interest] Clean way to define and categorize constants in
>>> QML
>>>
>>>
>>>
>>> Hello!
>>>
>>>
>>>
>>> What is a clean way to define and categorize constants in QML?
>>>
>>>
>>>
>>> I have came up with this so far:
>>>
>>>
>>>
>>> I have a QML singleton element, named Theme. There I keep a few constant
>>> things, like different background and font colors.
>>>
>>>
>>>
>>> Theme.qml:
>>>
>>> pragma Singleton
>>>
>>> import QtQuick 2.0
>>>
>>> QtObject {
>>>
>>>     readonly property color backgroundRed: "#FF2510"
>>>
>>>     readonly property color backgroundWhite: "#F0F0F0"
>>>
>>>     readonly property color backgroundPurple: "#930083"
>>>
>>>     readonly property color fontRed: "#FF1010"
>>>
>>>     readonly property color fontWhite: "#F6F6F6"
>>>
>>> }
>>>
>>>
>>>
>>> When I want to use one of the colors I write:
>>>
>>>
>>>
>>> color: Theme.backgroundRed
>>>
>>>
>>>
>>> I don't like this approach. What I would like to be able to write is
>>> this:
>>>
>>>
>>>
>>> color: Theme.background.red
>>>
>>>
>>>
>>> I have tried adding nested QtObjects inside Theme but I could not get it
>>> working.
>>>
>>>
>>>
>>> QtObject {
>>>
>>>     QtObject {
>>>
>>>         id: *background*
>>>
>>>         readonly property color red: "#FF2510"
>>>
>>>         readonly property color white: "#F0F0F0"
>>>
>>>         readonly property color purple: "#930083"
>>>
>>>     }
>>>
>>>
>>>
>>>     QtObject {
>>>
>>>         id: *font*
>>>
>>>         readonly property color red: "#FF1010"
>>>
>>>         readonly property color white: "#F6F6F6"
>>>
>>>     }
>>>
>>> }
>>>
>>>
>>>
>>> When I run it like this, I get an error: Theme.qml: Cannot assign to
>>> non-existent default property.
>>>
>>> Is there a way to accomplish this? Is there any clean way to define and
>>> categorize constants in QML?
>>>
>>> Thank you,
>>> Viki
>>>
>>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160429/df424ba4/attachment.html>


More information about the Interest mailing list