The only noticeable drawback of using QObject as an element of the model is that the base class is quite large, it is a kind of “object-god” (which is an anti-template), which contains a lot that you do not need most of the time. As a result, it has about 160 bytes of "overhead" on top of any model data you may have. This can be problematic if you have a large model with a large number of items, and the elements themselves are relatively small. As a result, you have a lot of overhead.
A QObjectList as a model is always a bad idea unless you are doing something completely trivial. Since it does not implement the proper interface for notifying you of links to change views, the only way is to force update, which will redraw the entire model each time, not just the changes.
There are no requirements for objects of objects if you correctly implement the model.
The second implementation is especially useful for a number of reasons:
- You don’t need to worry about implementing a specific “static” model with fixed roles for each use case.
- the elements of your model can have fundamentally different properties, you are not limited to the "scheme" model.
- you automatically get binding notifications in QML, as you deal with
QObject and Q_PROPERTY - you can define models in declarative, and you can even set models to create tree structures that you cannot do with
ListModel . - you can define the actual model elements in pure QML without recompiling all the time, aka rapid prototyping, and when that is done, you can just port the objects to C ++
- at the same time, in addition to all the advantages, the model is actually much easier to implement and maintain than the usual “rigid” model, the role search is faster, since you essentially have one
object role and there is no search, there is no need to implement data change signals for roles etc ... easy peasy
dtech source share