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.
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 |
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments