[Development] QTCS2019 Notes from QtQml session
Ulf Hermann
ulf.hermann at qt.io
Tue Nov 26 12:47:39 CET 2019
> Lookup by ID? You mean set the object name like you did in your example
> in your talk at QtWS? I protested then, and I will protest now again.
> Don't. Really: don't. If you are talking about re-usability and
> maintainability of your code elsewhere, then advocating this practice is
> really wrong. You need to layer your QML on top of an API provided by
> C++, not poke into your QML from C++.
Maybe that example in the slides was not so great. However, consider:
// main.qml
Item {
required property string title
}
// somewhere.cpp
QQmlComponent component(&engine, ":/main.qml");
QObject *obj = component.createWithInitialProperties(
QVariantMap { {"title", "Hello World"} });
This declares the property in QML and requires you to set it when
instantiating the component. The object won't be created if you fail to
set the property. We may improve on this in the future. QQmlComponent
already has a separate setInitialProperties() function, and we might add
another one to check if all required properties are set.
I had to introduce the replacements for context properties in some
order, and, apparently, introducing required properties first would have
been better. Sorry for the confusion.
Ulf
More information about the Development
mailing list