Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

ItemView framework

From Qt Wiki
Revision as of 06:39, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

h1. Interview framework

The Interview framework consists of a set of classes that are designed to facilitate the display of large numbers of items in the UI of an application. See the "Qt Whitepaper":http://developer.qt.nokia.com/wiki/QtWhitepaper (near the end) and the "overview page":http://doc.qt.nokia.com/4.6/model-view-programming.html in the documentation for a first overview.

Components

The basic architecture of Qts Interview framework are the models (subclasses of QAbstractItemModel) on the one hand, and the views (subclasses of QAbstractItemView) on the other. The models play the role of the data provider, while the views are responsible for the layout of the pieces of data into a useful representation. The third piece in the Interview framework is the concept of the delegate. A delegate has two tasks: rendering an item into the space allotted for the contents by the view, and providing an editor widget if needed.

Furthermore, there are two important helper components. The QModelIndex class is used to identify items in the model or view, and a QItemSelectionModel is used to keep track of which items in a view (or between views) is or are selected.

Models

The base class for all Interview models is QAbstractItemModel. This class provides the standard interface that allows all types of views. It is a two dimensional, hierarchical setup. Each item has an "address" that consists of a row and a column number, and a parent item. This "address" or index is contained in a QModelIndex instance. The root of the hierarchy is an invalid QModelIndex. In principle, every item in the model can have rows and columns of children. However, there are no views available in Qt that can display hierarchies in which the parent item is not an item in the first. The most complicated data structure that is used in practice is therefor the tree-layout, in which only items in the first column have children. Multiple columns are supported in that setup, but usually the item in the first column represent the main piece of information and the other columns provide additional information on the item in the first column in this setup.

Structures for data models. Taken from Qt Documentation.

Table-type models are a special case of the tree model described above, where none of the items have children. This results in a simple grid-type layout in which each row has the same number of columns. A list-type model is a special case of this table-type model, in which there is only one column for each item.

The versatility of the design makes it tricky to write a good model based on QAbstractItemModel. If you do not need the tree-semantics, you are probably better of using one of the standard provided abstract specializations below.

Note that the abstract models in Qt are not meant to store any data. They should be used to provide a standard interface to data that you have in your own data structures already! It is strongly discouraged to create models that do the actual storing of data inside the model implementation itself.

Standard provided abstract models

Qt provides a number of standard abstract models. Abstract models are models that require subclassing to be useful. These models are specializations of the basic QAbstractItemModel.

QAbstractListModel is the most basic abstract model that is available. It is useful for data that has list semantics: one column and no child-items.

QAbstractTableModel is an abstract model that provides table semantics. It is very much like the QAbstractListModel, but it allows you to use multiple columns.

Standard non-abstract models

Qt not only provides abstract models, but also provides a few models that do not require subclassing to be useful. The most basic model is QStringListModel. It provides a model with list-semantics that uses a QStringList to contain the data for each item.

QStandardItemModel provides a model that gives a pretty close match to the old Qt 3 way of working with views. Each item is represented by a QStandardItem instance that can be added to the model. The model supports all structures that QAbstractItemModel supports.

QFileSystemModel provides a Model/View interface to the systems file system. Using this model, you can display files and directories in the standard item views of Qt, making it easy to provide your own embedded file chooser for instance.

Interesting 3rd party models

Views

Delegates

Proxies

Convenience classes

Performance