Qt 5.12 - Model / View model / view (MVC) pattern

1 Introduction

1.1 "Mastering Qt 5" [2] in the MVC Profile

Model: model data management, responsible for data requests and update
View: presents the user with data
Controller: handles the interaction between the Model and the View. It is responsible for providing the correct data View, View provides for the operation request from the Model.
This mode reduces coupling, a plurality of the same data can be displayed View, View layer Model layer without fear of data changes.

1.2 Qt in the Model / View Profile

Table, list and tree widgets UI is commonly used in assembly. Window There are two ways to get data.

  • Traditional way
    window contains little control, direct access control data. This directly but in some large projects will produce data synchronization problems.
  • The second way
    is to model / view mode, this mode, the window comprising a control containing no internal data. They standardized interface to access external data, thus avoiding duplication of data. At first glance, this seems very complicated, but once carefully studied, not only easy to grasp, and the many benefits of model / view programming has become more apparent.
    Here Insert Picture Description

1.3 Baidu Encyclopedia introduction

model / view mode is MVC.
MVC full name Model View Controller, the model (model) - view (view) - Abbreviation controller (Controller), and a software design model, with a service logic, data, a method of separating tissue interface display code, business logic to gather a member which, while improving and customization interfaces and user interaction does not need to rewrite the business logic. MVC was developed a unique structure for a conventional mapping input, processing and output of the logic functions in a graphical user interface.

2 designed, the core idea

2.1 mind

Model / View mode beginning of the design is to separate the form of visual controls and data.

2.2 The core idea

3 Qt model / view presentation

3.1 Standard Form

Standard form, containing visual controls, data is part of the visual control.
Here Insert Picture Description
Directly addressable by the control data, which directly and effectively in some applications. But the two versions of internal and external controls to data synchronization, and data tightly coupled expression makes unit testing difficult.

3.2 Model/View

View class external data (model) operates.
Here Insert Picture Description
Model / View mode to avoid the small form controls operation of the data synchronization issues arise.

3.3 Model / View Form Overview

Here Insert Picture Description
Here Insert Picture Description

3.4 Adapters between Forms and Models

Data stored in the editing table itself inside the table from, but much more comfortable and edit data directly in the text field. There is no direct model / view a copy of the data and separated by a small part of the view, the widget to a value (QLineEdit, QCheckBox ...) instead of the operation data set, so we need an adapter to connect to a data source form.
QDataWidgetMapper is a good solution, because it can map table rows to form widgets, and very easy to construct a form of database tables. Here Insert Picture Description
Another example of the adapter is QCompleter. Qt has QCompleter, for (e.g. a QComboBox) and provides auto-completion QLineEdit, as shown in Qt widget. QCompleter using the model as its data source.
Here Insert Picture Description

4 advantages and disadvantages

4.1 advantage

  • Reduce the coupling operation of visual control data
  • Avoid multiple controls reading and writing data consistency problems arise

4.2 shortcomings

  • Small projects to increase the amount of code

5 Module Introduction

5.1 TreeView

QListView, QTableView and QTreeView use abstract model that combined lists, tables and trees. This allows the use of several different types of view class in the same model.
Here Insert Picture Description
QStandardItemModel hierarchical data for a container, the container is also achieved QAbstractItemModel. To display the tree, must be filled with QStandardItemModel QStandardItems, QStandardItems can save all the standard properties of the project, such as text, fonts, check box or brushes.

5.2

5.3

Example 6

7 understanding

Here Insert Picture Description
The MVC (view / model structure) data and view component separation, which allows us to display the same data in several different attempts to assembly, and to achieve a new type of view, and does not change the underlying data structure. It may be, create a different view of the same device region patterns depending on control response command, to load the different input data. For a more flexible user input processing, the introduction of the concept of the delegate. Its advantage is that rendering and programming data items can be customized.
Here Insert Picture Description
model indexes acquired from the view model, which is a reference to a data item. , View data may be obtained from the data source provided by the model indexes to the model. In the standard views in, delegate would render the data item, when a data item is selected, delegate to communicate directly through the model indexes and model.

Communicate slots mechanism - the signal between Models, views, Delegates:
(. 1) signals emanating from the notification data model view data source changed.
(2) The signal is emitted from the view it provides information about the interaction with the user data item to be displayed.
(3) The signal generated from a delegate is used to inform about the current state of the model and view information editor while editing.

Models (model):

    所有的item models都基于QAbstractItemModel类,这个类定义了用于views和delegates访问数据的接口。数据本身不必存储在model,数据可被置于一个数据结构或另外的类、文件、数据库、或其它的程序组件中。

   QAbstractItemModel提供给数据一个接口,它非常灵活,基本满足views的需要,无论数据用以下任何形式表现,如table、list、trees。然而,当你重新实现一个model时,如果它基于table或list形式的数据结构,最好从QAbstractListModel、QAbstractTableModel开始做起,因为它们提供了适当的常规功能的缺省实现。这些类可以被子类化以支持特殊的定制需求。
    Qt提供了一些现成的models用于处理数据项:

(1) .QStringListModel simple list of QString for storing items.
(2) .QStandardItemModel manage more complex tree structure data items, and each can contain any data.
(3) .QFileSystemModel provides file and directory information in the local file system.
(4) .QSqlQueryModel, QSqlTableModel, QSqlRelationTableModel used to access the database.

   如果这些标准模型不能满足需要,可以使用子类化QAbstractItemModel、QAbstractListModel、QAbstractTableModel类来定制模型。
  • The Views (view):
    a different view are achieved complete their respective functions, QListView the data is displayed as a list, QTableView Model data table is shown in the form of, QTreeView having a hierarchical list to display the data in the model. These classes are based QAbstractItemView abstract base class. Although these classes have been fully realized, but they can still be used to subclass to meet custom needs.
  • Delegates (delegate):
    QAbstractItemDelegate is an abstract base class for a delegate model / view architecture. The default implementation provides the QStyledItemDelegate delegate class, which can be used to delegate the default standard views of the Qt. However, QStyledItemDelegate QItemDelegate and is independent of the method for the view (views) drawing of the items and editing functionality. In that both of them different, QStyledItemDelegate the current style (style) to describe items. Therefore, when we implement a custom delegate (delegate) or use Qt Style Sheets, we recommend the use of QStyledItemDelegate as a base class.
  • Sorting (Sort):
    In the model / view architecture, there are two ways to sort and choose which method depends on your underlying Model. If your model is ordered, that is, it re-implemented QAbstractItemModel :: sort () function, QTableView and QTreeView provides API, allows you to programmatically sort of model data. Furthermore, you can also sort the interactive mode (e.g., allowing the user to view the header by clicking a way to sort the data), specific method is: the QHeaderView :: sectionClicked () signal QTableView :: sortByColum () or grooves QTreeView :: sortByColumn () to associate groove.
    Another way is, if you do not provide the required interfaces model you want to represent data or list view, a proxy model may be used to convert your model data structure before the data represented by view.
  • Convenience classes (class convenience):
    Many convenience classes are derived from the standard view classes that facilitate the use of those items Qt-based view and table classes, they should not be subclassed, they only provide for the equivalence classes Qt 3 a familiar interface. These classes have QListWidget, QTreeWidget, QTableWidget, as they provide similar Qt 3 in QListBox, QlistView, QTable behavior. These analogies View class lacks flexibility and can not be used for any Models, recommended model / view of the data processing method.

reference

. 1, the Qt the Model / View model / view use summary (a)
2, the Qt the Model / View using the model / view (b)
3, a deep understanding model view, custom model
. 4, 38.Qt model / view of the structure
5, in-depth understanding the MVC
. 6, the official - Model / View the Tutorial
. 7, QT development (thirty-eight) - Model / View programming framework
. 8, the Qt the Model / View appreciated that (a) - configuration Model
. 9, the Qt official - Model / View programming
10, the MVC frame
11, QT development (thirty-seven) - Model / View official documents

Published 496 original articles · won praise 601 · Views 1.55 million +

Guess you like

Origin blog.csdn.net/qq_38880380/article/details/103934965