[Development] QTCS2019 Notes from QtQml session

Arno Rehn a.rehn at menlosystems.com
Tue Nov 26 20:32:20 CET 2019


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.

Regards,
Arno

-- 
Arno Rehn
Tel +49 89 189 166 0
Fax +49 89 189 166 111
a.rehn at menlosystems.com
www.menlosystems.com

Menlo Systems GmbH
Am Klopferspitz 19a, 82152 Martinsried, Germany
Amtsgericht München HRB 138145
Geschäftsführung: Dr. Michael Mei, Dr. Ronald Holzwarth
USt.-IdNr. DE217772017, St.-Nr. 14316170324


More information about the Development mailing list