QtOnAndroid

Model/View Pattern with Qt

Qt Native Orientation Sensor | | Qt Model-View Example

Lecture5

The Model-View programming pattern is helpful to separate the view (the ui) from the underlying model (the data). This has the following advantages:

  • If the view layout is changed, the data container does not have to be modified.
  • Model/view eliminates the data consistency problems that may occur with standard widgets.
  • It also avoids data duplication, because more than one view of the same data can exist.

Since view classes do not know your data’s structure, you need to provide a wrapper that is the interface to the data. With Qt your data container needs to conform to the QAbstractItemModel interface. The data view can use this interface to read from and write to your data. Any instance of a class that implements QAbstractItemModel is said to be a model. Once the view receives a pointer to a model (via setModel(…)), it will read and display its content and be its editor. The component which provides the editing logic is called a delegate. The default delegate handles number and string editing tasks. It can be replaced with a custom editor.

In Qt the following common view types are Model/View enabled:

  • list view (QListView)
  • table view (QTableView)
  • tree view (QTreeView)
  • and column view (QColumnView)

Note: The old-style “Model/View/Controller” pattern is no longer regarded appropriate from a modern software-engineering point of view. In many practical cases a large fraction of the control logic is an inherent part of the model, so it is difficult to separate it out completely.

Qt Native Orientation Sensor | | Qt Model-View Example

Options: