Qt model/view using model item : is specific view item required?
I'm about to implement something similar to : http://doc.qt.nokia.com/latest/itemviews-simpletreemodel.html
I have a type, aosl::Object, that contains all the data of a node in a tree of Objects.
I want to display some informations in a QTreeView. So it seems I need to implement a class inheriting from QAbstractItemModel to allow the views to exploit the data. So far so good. That class would be like the TreeItemModel class in the example.
开发者_JS百科However, in the example, they also implement a TreeItem class that seems to hold the data required for the tree node. It is not a QObject or something associated with Qt - that's where my doubt comes from.
So the question is : do I need to have another type than my aosl::Object type inside my QAbstractItemModel, or can I use it as is withtout having to add another "item" type?
QAbstractItemModel's external interface is operating soley on QModelIndex and QVariant - no internal structure is exposed to view class. As long as your class (aosl::Object) can provide all information required in implementation of your QAbstractItemModel decenant you don't need to wrap it into QObject, or anyhow else.
If you look at the class TreeModel
, you see it acts as the bridge between TreeItem
and QAbstractItemModel
. Using this pattern, TreeItem
doesn't depend on QAbstractItemModel
, nor does QAbstractItemModel
depend on TreeItem
.
This is a useful pattern, because it makes your application less complex. In your case, for instance, it means that your aosl::Object
class doesn't need to be changed just because you want to show it in a (QAbstractItemModel) tree.
精彩评论