[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
Hi,
I have a list of objects at the core of a class inheriting
QAbstractListModel,
QList<MyObject*> m_list;
"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:
Item {
id: theDelegate
property string someValue: myObject.someObjectValue
}
The properties have proper bindings, so the properties are updated when
something changes.
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,
Item {
id: theDelegate
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.
Cheers,
Ola
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131218/3eff514f/attachment.html>
More information about the Interest
mailing list