[ fromfile: qtmodelview.xml id: qtmodelview ]
Qt includes a Model/View Framework that maintains separation between the way data is organized and managed, and the way that it is presented to the user. Classes for the three most common types of views (lists, trees, and tables) are provided. In addition, there are abstract and concrete data models that can be extended and customized to hold different kinds of data. It is not unusual for an application to have a model that can be viewed in several different ways, simultaneously.
Views are objects for acquiring, changing, and displaying the data. Figure 13.3 shows four kinds of View types in the Qt model-view framework.
QAbstractItemModel defines the standard interface that enables views (and delegates) to access data. Item models store the actual data that is to be viewed and manipulated (e.g., sorted, edited, stored, retrieved, transmitted, and so on). Using signals and slots, they notify all associated views of any changes to the data. Each view object holds a pointer to a model object. View objects make frequent calls to item model methods to get or set data or to do various other operations. Figure 13.4 shows the model classes that are designed to work closely with the various view classes.
Selection models are objects that describe which items in the model are selected in the view. Each view has a pointer to a selection model. QModelIndex acts like a cursor, or a smart pointer, providing a uniform way to iterate through list, tree, or table items inside the model.
After setModel()
is called, the view should automatically update itself whenever the
model changes (assuming the model is written properly).
There are two approaches to implementing the data model. Each has advantages.
Implement the passive interface of a QAbstractItemModel, including the data representation.
Reuse a general-purpose concrete data model, such as QStandardItemModel and fill in the data.
The passive interface offers more flexibility in implementation. It is possible to use data structures that are optimized for specific access patterns or data distributions.
The second approach, reusing the QStandardItem(Model) classes, makes it possible to write tree/item code in a style similar to that used by QListWidget, QTableWidget, and QTreeWidget.
QAbstractItemView provides an interface for the common features of the three different Model types:
Lists in various arrangements
Tables, perhaps with interactive elements
Trees representing objects in a parent-child hierarchy
Each piece of data in a model is represented by a model index. Model indexes give views and delegates indirect access to data items in the model without relying on knowledge of the underlying structure of the data. Only the model needs to know how to directly access its data. The QModelIndex class provides an interface for indexing and accessing data in models derived from QAbstractItemModel. It works equally well for lists, tables, and trees. Each index has a pointer to the model that created it, and may have a parent index, in case of hierarchically structured data (e.g., in a tree). QModelIndex treats model data as if it were arranged in a rectangular array with row and column indices, regardless of what underlying data structure actually holds the data.
QModelIndex objects, created by the model, can be used by model, view, or delegate code to locate particular items in the data model. QModelIndex objects have short lifespans and can become invalid shortly after being created, so they should be used immediately and then discarded.
QModelIndex::isValid()
should be called before using a QModelIndex object that has
existed for more than a few operations.
QPersistentModelIndex
objects have longer life spans but still should be checked with isValid()
before
being used.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |