Qt development summary (14) - Model / View framework

Part summarized also mentioned, there is a class Qt common UI controls, such controls using a technique referred to as Qt Model / View framework. Each UI developer should understand ModelView programming, this technique involves a series of list, table, and tree controls tree structure. The view class using the model / view architecture to manage the relationship between the data and its display. This feature structure into the separation system to provide greater flexibility for developers can be defined from the representation of the project, and provide a standard interface model, to allow a variety of data sources used with existing project view.

Model / View framework

Model-View-Controller (MVC) mechanism is a schematic view showing the relationship of the user data and the management of a kind commonly used, is typically used when constructing a user interface. model is the management of data objects, view data is displayed to the user, Controller provides the data processing interface. MVC Before use, the user data processing interface design together with the data object, the MVC simplifies this operation, to thereby increase a lot of flexibility.

If you connect with View and Controller, then that model / view mechanism. It still uses the principle of separation and data and views, but it provides a simpler framework. This separation will make the same data as possible throughout the several views, and re-implement the new data, but does not need to change the structure of the data. For flexible user input, we now introduce the concept of delegate (proxy) of. The advantage of using such delegate is deledate data items may be edited and customized.

, Model / view mechanism, model shown in FIG responsible for communication with the data source, provides an interface to other elements in the mechanism. Get index view (index) from the model, constraint data item in the data source. In the standard views in, delegate in charge of the modified data item, when an item has been edited, delegate model index directly communicate with the model.

 

In summary, model / view classes can be divided into three parts: model, view, and delegate. Each portion is defined in the abstract classes. (Abstract classes to provide a common interface and some default action). abstract classes can be used to inherit, to provide richer functionality for each model, view or delegate through inheritance.

model, used to communicate between the groove and the signal between the view, and delegate.

1 signaling data from the model view has changed

2. The signal from the view of the user information items interaction

3. The signal from the delegate is to notify the user during the model and view edit mode to edit

Models (model)

All models classes inherit from QAbstractItemModel class. This class defines the interface views and delegates access to data. Data itself is not in the model, which may be in a particular data structure or a file, database or other storage elements in storage applications.

QAbstractItemModel flexible enough to provide some interface view (tables, lists, trees) interaction. However, when used list and table structures, QAbstractListMode QAbstractTableModel class and will be more convenient. Because they provide appropriate default action is a public function. These classes can be inherited to, lists, and for the specific needs of the tables provided for the models.

Qt provides some already defined models:

QStringListModel is used to store a list of items simple QString

QStandardItemModel management tree structure more complex items, each data item may comprise any

QFileSystemModel provide some information on the local file system files and directories

QSqlQueryModel, QTableModel, and QSqlRelationTableModel be used for model / view acquired data in the database

If these standard models do not meet your needs, you can inherit QAbstractItemModel, QAbstractListModel or QAbstractTableModel to construct their own models.

QStringListModel

Stores a list of strings

QStandardItemModel

Stores arbitrary hierarchical items

QFileSystemModel

Encapsulate the local file system

QDirModel

QSqlQueryModel

Encapsulate an SQL result set

QSqlTableModel

Encapsulates an SQL table

QSqlRelationalTableModel

Encapsulates an SQL table with foreign keys

 

Views (View)                                        

Be achieved by three different views: QListView display list items, QTableView the display data in the model table, QTreeView the display data in the tree list. These classes are inherited from QAbstractItemView. Although these classes are already being used to achieve a particular purpose, but they can also be inherited to customize views. I will describe how to use these controls specific classes later.

Delegate (Acting)

QAbstractItemDelegate a model / view a mechanism abstract base class delegates. Achieved by default QStyleItemDelegate delegate class, which is used as the default standard views of the Qt delegate. We recommend QStyleItemDelegate as the basic category when the implement custom delegate.

Widget (Control)

Derived from the standard view class many convenience class for the benefit of the application to item-based item view and table classes depend on Qt. These classes are mainly QListWidget, QTreeWidget and QTableWidget. Inferior flexibility of these classes view class, and can not be used with any model. And they can not be inherited. We recommend that you use the model / view method to handle data items in the view, if you really want while still based interface project using the functional model / view approach provides, consider the view class (for example QListView, QTableView and QTreeView ) used in conjunction with QStandardItemModel. Here are some common controls:

Control schematic

The class name

Model / view framework

QListWidget

QListView

QTableWidget

QTableView

QTreeWidget

QTreeView

 

QColumnView shows a tree as a hierarchy of lists

QComboBox can work as both a view class and also as a traditional widget

 

summary

So, Mode / View provides a solution to use more generic architecture. Mode / View elimination of standard controls data consistency problems that may arise. Mode / View can also be easier to use multiple views of the same data, because the model can be transferred to a plurality of views. The most important distinction is the Mode / View control cells not later storage data table. In fact, they operate directly from your data. Because the view class does not know the structure of the data, it is necessary to provide a package so that the data conform QabstracteModel interfaces. View use this interface to read and write data. QabstratemModel achieve any instance of the class are referred to as model. Upon receiving the pointer to view the model, it reads and displays its contents, and becomes its editor.

发布了76 篇原创文章 · 获赞 63 · 访问量 5万+

Guess you like

Origin blog.csdn.net/bjtuwayne/article/details/103450181