In Qt there is a class QAbstractTableModel
, its an abstract class. Why Qt designers provide an abstract class, and doesn't provide an actual class that can be used for modelling a table. Why the designer made me to subclass the class to use it?

- 47,916
- 17
- 112
- 190

- 13
- 4
2 Answers
That's because that class is not used for modelling a table, it's an interface that all classes that model a table must adhere to. Qt has a few concrete models that you can reuse, simply look at the "Inherited by" list in the documentation of the base QAbstractItemModel
class. Some of these derived classes are concrete, namely those whose names don't start with QAbstract
:)
If you want a generic model, you can use a QStandardItemModel
.
You'll need to read up about interfaces in C++. An interface implemented using abstract virtual methods is a very common idiom. See e.g. here. In C++11 you can have interfaces that don't use the virtual method idiom, though.
If you ask "why doesn't Qt provide any general-purpose concrete classes that implement that interface", the answer is: because it's an impossible job. Everyone's data source has different implementation details, and Qt can't possibly divine everyone's approach and provide a universal bridge.
The QAbstractTableModel
exists to let you create an adapter between your own data model and Qt's data model.

- 95,931
- 16
- 151
- 313
-
why all the classes that model a table should adhere to that interface ? is there something that is used for the view that adhere to that interface ? – andreahmed Apr 14 '16 at 18:38
-
1@andreahmed Because without an interface it's impossible to write a universal view or proxy to display/modify the data. – Kuba hasn't forgotten Monica Apr 14 '16 at 18:51
There is universal model implementation such as QStandardItemModel, so you can use this for table, tree or list views as you need.

- 4,249
- 18
- 28