[Development] QTCS2019 Notes from QtQml session

Milian Wolff mail at milianw.de
Wed Nov 27 11:12:56 CET 2019

On Dienstag, 26. November 2019 20:32:20 CET Arno Rehn wrote:
> On 26/11/2019 08:56, Ulf Hermann wrote:
> >> We have some code that evaluates JS in custom QQmlContexts with certain
> >> "magic" context properties set (sort of like the "index" or "modelData"
> >> context properties in delegates like Repeater.delegate).
> >> Will something similar still be possible?
> > 
> > You should rephrase that as required properties on actual objects. Then
> > the magic won't work anymore and you'll have to look up the properties
> > by ID of the object, but that is a good thing. It will improve
> > re-usability of your components.
> Right, that would work in this case. Thanks!
> Anyway, how would I idiomatically register a bunch of dynamically
> created C++ objects with QML then? As an example, we're automatically
> detecting hardware stuff and creating interface objects based on that.
> These interface objects are then passed to QML which we use to implement
> state machines.
> Pseudo-code example:
>   QObjectList objects = detectAndCreateObjects();
>   QVariantMap qmlObjects;
>   for (auto object : objects) {
>       qmlObjects[object->objectName()] = object;
>   }
>   engine->rootContext()->setContextProperty("Objects", qmlObjects);
> Now I see two (maybe more) "workarounds" for that:
> 1. Register every object as a singleton. That's bad design, because
> these objects don't really match the singleton pattern.
> 2. Register a single accessor singleton instance which holds the
> qmlObjects. That's a lot more to type, though:
>   import QtQml.StateMachine 1.0 as DSM
>   import Utils 0.1  // provides "ObjectProvider"
>   DSM.State {
>     onEntered: ObjectProvider.objects.foobar.frizzle()
>     // In QML 2:
>     // onEntered: Objects.foobar.frizzle()
>     // or even just:
>     // onEntered: foobar.frizzle()
>     // if we leave out the QVariantMap above
>   }
> I realize that this makes things more "well defined". But I think it'll
> make usage of QML as a domain-specific language (like for implementing
> state machine logic) a little more cumbersome.

The third option is to add a `required property` on your root object and 
instantiate your context with the `qmlObjects` directly. Then you pass that 
through to your state machine.

Both of these options are "more typing" but at the same time the code becomes 
more explicit and thus easier to refactor and interpret by tools.

Milian Wolff
mail at milianw.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/development/attachments/20191127/ab3604b5/attachment-0001.sig>

More information about the Development mailing list