What are the advantages of QAbstractItemModel over QStandardItemModel?
Asked Answered
C

1

8

I want to store custom data using a model. For that, I am a bit confused as to which item model to choose for subclassing. I need some clarification as to which is the best model for subclassing? And what are the advantages of QAbstractItemModel over QStandardItemModel?

Cousin answered 9/5, 2012 at 6:14 Comment(0)
B
2

It depends on your needs. Use QStandardItemModel if you just want to store custom data and don't want to write your own model logic. This one is generic, you can use it for custom data without subclassing.

On the other way, if you wish to write your own model logic, then choose QAbstractItemModel. It is abstract class. It means, it has no behavior implemented, it is just an 'interface'. It tells you which methods your model should implement, so it can be used as ano other ItemModel class in Qt.

Bomb answered 9/5, 2012 at 6:28 Comment(5)
Thanks kousalik ! Any other major differences ?Cousin
No, QStandardItemModel is a implementation (subclass) of QAbstractItemModelBomb
@Bomb could you possibly expand on what you mean by 'model logic'? What specific model logic does QStandardItemModel implement, which isn't done by the AbstractItemModel? E.g., if you want your model to be editable, is it easier with a StandardItemModel? I'm just curious because I have been using QAbstractItemModel, and am thinking about why I should or shouldn't switch to QStandardItemModel.Medina
How have you used the abstract model ? It cannot be just used. Quoting its docs: The QAbstractItemModel class defines the standard interface that item models must use to be able to interoperate with other components in the model/view architecture. It is not supposed to be instantiated directly. Instead, you should subclass it to create new models. End of quote. The difference is that the Standard model already implements some basic behavior for showing standard data. For more comlex structers, filterin, combining or whatever you have to implement your own logic and therefore go for subclassingBomb
Kousalik I use abstractitemmodel for tree views, so I think I understand what you meant, but someone asking this question might not understand what you mean by 'model logic,' which forms the conceptual fulcrum of your answer.Medina

© 2022 - 2024 — McMap. All rights reserved.