Qt 4 Models and Views

Table of contents:

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

  • Lists in various arrangements
  • Tables, perhaps with interactive elements
  • Trees representing objects in a parent-child hierarchy
  • Graphs and charts

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



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net