[Interest] Best practice for properties, QAbstractListModel with a QList<QObject*> and a Quick2 view
Ola Røer Thorsen
ola at silentwings.no
Wed Dec 18 11:59:15 CET 2013
I have a list of objects at the core of a class inheriting
"MyObject" inherits QObject and has many properties (20 or so). I need to
access most of these properties in a Quick2 view delegate.
To avoid creating roles for each of these properties, I created a
"myObject" role in the list model class instead, that returns a QVariant
containing the pointer to the object.
This works allright as long as the model count doesn't change. In a
delegate, I can get the properties like so:
property string someValue: myObject.someObjectValue
The properties have proper bindings, so the properties are updated when
However I get problems when I delete objects from the list model. The
delegates are deleted later than the actual objects, so I get warnings from
QML on the console (saying myObject is null). These seem harmless enough,
but I would like to avoid having any warnings at all.
I can avoid them by doing tests where I use myObject in QML like so,
property string someValue: myObject ? myObject.someObjectValue : ""
but this is just ugly.
So I wonder, how should I best deal with situations like this? Is exposing
the object pointer a bad practice to begin with?
The alternative is to create roles for every property in MyObject, and
connect signals for every one of them so the model updates when one of the
properties changes. Perfectly doable of course, but it has to be kept
up-to-date with the properties in MyObject, and feels like duplicating a
lot of code.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Interest