8

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

Thanks!

eric
  • 7,142
  • 12
  • 72
  • 138
Dev
  • 127
  • 2
  • 8

1 Answers1

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.

Kousalik
  • 3,111
  • 3
  • 24
  • 46
  • Thanks kousalik ! Any other major differences ? – Dev May 09 '12 at 06:34
  • No, QStandardItemModel is a implementation (subclass) of QAbstractItemModel – Kousalik May 09 '12 at 06:44
  • @Kousalik 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. – eric Dec 01 '14 at 18:53
  • 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 subclassing – Kousalik Dec 03 '14 at 07:00
  • 1
    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. – eric Dec 04 '14 at 18:52