[Development] QTCS2019 Notes from QtQml session
Ulf Hermann
ulf.hermann at qt.io
Wed Nov 27 09:48:28 CET 2019
> 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.
You can have a QJSValue as singleton. There is a
qmlRegisterSingletonType() that takes a QJSValue factory function. That
QJSValue can be an object (QJSEngine::newObject()) and the object can
have arbitrary properties, keyed by strings. Note that you should really
only do this if you need all the flexibility of creating a completely
unpredictable "bag of objects". You're deferring all the type checking
and property resolution to run-time this way.
In QML that would be:
import QtQml.StateMachine 1.0 as DSM
import Utils 0.1 // provides QJSValue singleton "Objects"
DSM.State {
// just as if "Objects" were a context propery
onEntered: Objects.foobar.frizzle()
}
Ulf
More information about the Development
mailing list