Qt 4 Models and Views

Qt 4 offers general-purpose view classes for the most common types of views: lists, trees, and tables. This includes abstract and concrete data models that can be extended and customized to hold different kinds of data.

Figure 17.8 shows the four main types of classes in the Qt 4 model-view framework. Each class has a specific role.

  1. Item models are objects for representing a data model for multiple items. Item models store the actual data that is to be viewed/manipulated.
  2. Views are objects for acquiring, changing, and displaying the data. Each view holds a pointer to a model. View classes make frequent calls to item model methods to get and set data.
  3. Selection models are objects that describe which items in the model are selected in the view. Each view has a selection model.
  4. QModelIndex acts like a cursor, or a smart pointer, providing a uniform way to iterate through list, tree, or table items inside the model.

Figure 17.8. Qt 4 Model/View classes

The code fragment below shows how to create and connect a view to a model.

class MyTableModel : public QDefaultTableModel {...}; // ... myModel = new MyTableModel(); myView = new QTableView(); myView->setModel(myModel);

After setModel() is called, the view should automatically update itself whenever the model changes (assuming the model is written properly).

As we write the implementation of MyTableModel, we are once again implementing a passive interface, and there is an inversion of control. All the methods we override in MyTableModel are called from QTableView or QModelIndex.

Abstract or Default?

When extending QAbstractxxxModel, derived classes need to override all of the pure virtual methods and define a full implementation for the model. In contrast, by extending one of the QDefaultxxxModel classes, a derived class inherits a default implementation that does not require overrides of all methods. Each method has an empty stub in the default base class.

 

Views

A View class encapsulates the components of a graphical user interface that accesses the data in a model. Views come in a variety of different sizes and shapes. In general, they are usually

Model Index

The QModelIndex class provides a generic access system that works for all classes derived from QAbstractItemModel. This system 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 life spans 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.

Table Models

Категории