2 Model View
Eelke Klein edited this page 2018-08-26 08:42:55 +00:00

The Model View system of Qt allows you to link a Model to a View but it is much more then that. This page describes how the pieces fit together.

What parts are there:

  • The view ie QTableView
  • The model, for instance a descendant of QAbstractTableModel
  • QHeaderView, is responsible for the headers both above the columns and in front of rows
  • QItemSelectionModel the view has a selection model
  • QAbstractItemDelegate views use delegates to render items (on table can be set per row, column or globally)

You can get quite good results with just a view and a model. In this case the default delegate retrieves formatting info from the delegate. However formatting should be part of the view not the model.

Ways to seperate formatting from the model

  • We can use UserRoles to allow model to return what kind of data an item is (sometimes this can be deduced from the type stored in the variant but sometimes you might need more). Then a custom delegate could use that information to decide on the formatting.
  • We can go crazy with custom delegates and make sure the view is using the right delegate but that would mean that the view needs to be initialized with the right delegates and this might need to be updated when the model is changed.

The first method seems to be more flexible as it means we can probably use the same delegate everywhere so setting up a view correctly will still be easy. In special cases we can still use a more specific delegate.