Why model/view is preferred to widget?

QListView, QTableView and QTreeView all use a model abstraction, which is a merged list, table and tree. This makes it possible to use several different types of view classes from the same model.

Why model/view is preferred to widget?_第1张图片

Table, list and tree widgets are components frequently used in GUIs. There are 2 different ways how these widgets can access their data. The traditional way involves widgets which include internal containers for storing data. This approach is very intuitive, however, in many non-trivial applications, it leads to data synchronization issues. The second approach is model/view programming, in which widgets do not maintain internal data containers. They access external data through a standardized interface and therefore avoid data duplication. This may seem complicated at first, but once you take a closer look, it is not only easy to grasp, but the many benefits of model/view programming also become clearer.

1. Introduction

Model/View is a technology used to separate data from views in widgets that handle data sets. Standard widgets are not designed for separating data from views and this is why Qt 4 has two different types of widgets. Both types of widgets look the same, but they interact with data differently.

Standard widgets use data that is part of the widget.

Why model/view is preferred to widget?_第2张图片

View classes operate on external data (the model)

Why model/view is preferred to widget?_第3张图片



1.1 Standard Widgets

Let's have a closer look at a standard table widget. A table widget is a 2D array of the data elements that the user can change. The table widget can be integrated into a program flow by reading and writing the data elements that the table widget provides. This method is very intuitive and useful in many applications, but displaying and editing a database table with a standard table widget can be problematic. Two copies of the data have to be coordinated: one outside the widget; one inside the widget. The developer is responsible for synchronizing both versions. Besides this, the tight coupling of presentation and data makes it harder to write unit tests.


1.2 Model/View to the Rescue

Model/view stepped up to provide a solution that uses a more versatile architecture. Model/view eliminates the data consistency problems that may occur with standard widgets. Model/view also makes it easier to use more than one view of the same data because one model can be passed on to many views. The most important difference is that model/view widgets do not store data behind the table cells. In fact, they operate directly from your data. Since view classes do not know your data's structure, you need to provide a wrapper to make your data conform to theQAbstractItemModel interface. A view uses this interface to read from and write to your data. Any instance of a class that implements QAbstractItemModel is said to be a model. Once the view receives a pointer to a model, it will read and display its content and be its editor.


1.3 Overview of the Model/View Widgets

Here is an overview of the model/view widgets and their corresponding standard widgets.

Widget Standard Widget
(an item based convenience class)
Model/View View Class
(for use with external data)
Why model/view is preferred to widget?_第4张图片 QListWidget QListView
Why model/view is preferred to widget?_第5张图片 QTableWidget QTableView
Why model/view is preferred to widget?_第6张图片 QTreeWidget QTreeView
Why model/view is preferred to widget?_第7张图片   QColumnView shows a tree as a hierarchy of lists
QComboBox can work as both a view class and also as a traditional widget

你可能感兴趣的:(Why model/view is preferred to widget?)