Im very new in Qt programming i want to display data with Qt TableView which im getting from XML file.
I Cannot find any useful tutorial about how to create custom model and then bind it to TableView.
Please provide some help or if anybody have some good examples please share.
Thanks
The model-view approach in Qt is quite versatile. All models inherit from QAbstractItemModel. With this class you can create quite complex data layouts (lists, trees, tables etc.) but the effort for the implementation is in comparison quite high, too.
A ready to use class would be
QStandardItemModel. You can easily create a table model and add the items, which are instances ofQStandardItem. You can use the following code to get started:You see, it is really easy to use. However, a drawback is that you have to supply the data via a
QStandardItem, which might be a waste of memory. For example, assume you have several 100MB of data, which you would like to display in a view. As you already have the data stored somewhere, it would be preferable to just adapt it such that it can be used in the view instead of creating aQStandardItemfor every cell.This is where
QAbstractTableModelcomes into play. The following example creates a matrixwith 250.000 entries. Instead of creating one
QStandardItemfor every matrix element,we sub-class
QAbstractTableModeland implement the three pure virtual methodsnumRows(),numColumns()anddata(), which return the number of rows, columns andthe data to display.
As you can see, the model does not duplicate any data but just serves as an adapter. If you need even more flexibility, you can go to
QAbstractItemModeland event implementthe creation of the model indexes, which Qt uses to specify which model data to read or
write.