openKylin论坛

 找回密码

Qt开发之model/view编程系列——(一)写在前面 [复制链接]

传送门:Qt开发之model/view编程系列
我一共写了七个章节,如果大家感兴趣可以移步简书,这是第一章。


写在前面


本书分几个章节,附相关示例代码链接:
示例代码
开发环境构建:
在UbuntuKylin 19.10上配置qt5和qtcreator
sudo apt install qt5-default qtcreator
如果不想使用qtcreator,可以使用qmake在终端进行构建。



自Qt4以来,Model/View编程一直是Qt中非常重要的一部分,什么是Model/View编程呢?Qt官方的帮助文档中有这样的描述:
The model/view architecture

Model-View-Controller (MVC) is a design pattern originating from Smalltalk that is often used when building user interfaces. In Design Patterns, Gamma et al. write:

MVC consists of three kinds of objects. The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input. Before MVC, user interface designs tended to lump these objects together. MVC decouples them to increase flexibility and reuse.

If the view and the controller objects are combined, the result is the model/view architecture. This still separates the way that data is stored from the way that it is presented to the user, but provides a simpler framework based on the same principles. This separation makes it possible to display the same data in several different views, and to implement new types of views, without changing the underlying data structures. To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized.

    The model/view architecture
The model communicates with a source of data, providing an interface for the other components in the architecture. The nature of the communication depends on the type of data source, and the way the model is implemented.
The view obtains model indexes from the model; these are references to items of data. By supplying model indexes to the model, the view can retrieve items of data from the data source.
In standard views, a delegate renders the items of data. When an item is edited, the delegate communicates with the model directly using model indexes.
Generally, the model/view classes can be separated into the three groups described above: models, views, and delegates. Each of these components is defined by abstract classes that provide common interfaces and, in some cases, default implementations of features. Abstract classes are meant to be subclassed in order to provide the full set of functionality expected by other components; this also allows specialized components to be written.

Models, views, and delegates communicate with each other using signals and slots:

Signals from the model inform the view about changes to the data held by the data source.
Signals from the view provide information about the user's interaction with the items being displayed.
Signals from the delegate are used during editing to tell the model and view about the state of the editor.

如果你知道MVC设计模式,那么你可以把qt的model/view理解成类似的东西,它的核心思想和MVC一样,意图做到数据与视图的分离,这样做的好处很明显——我们可以把一个model使用不同的view展示出来,这对一个开发者来说无疑是一件非常好的事情。
如果你之前没有接触过类似MVC的概念,那么下面这张图能够比较直观的反映它的部分理念



QFileSystemModel与各种View的结合


上图能够说明model与view的关系,对应到MVC中就是M和V的关系,那么MVC中的C在qt的model/view中是怎样的呢?这个图上我们没办法看明白,我先给出它的答案:
在MVC中,视图对数据的修改是由Controller实现的,在qt的model/view编程中,负责修改的角色是delegate;与MVC不同,delegate还承担了view item,也就是数据单元的绘制工作,所以delegate被认为是view的一部分。这也是qt的model/view编程与MVC编程的一个显式区别。



使用delegate进行特殊的绘制





delegate设置spinbox修改表格数据


我想现在你应该对Qt的model/view编程有一个初步的了解了,然而,想要真正的理解它并将其合理的运用,显然只有这些是远远不够的,接下来的文章我会针对model/view编程中比较核心的部分进行较为完善的分析,希望通读完这些文章后你能够将其吸收归纳成自己的知识,在qt开发的成长道路上更进一步。

楼主
发表于 2019-10-14 11:56:48
回复

使用道具 举报

openKylin

GMT+8, 2024-5-7 00:46 , Processed in 0.021494 second(s), 17 queries , Gzip On.

Copyright ©2022 openKylin. All Rights Reserved .

ICP No. 15002470-12 Tianjin

快速回复 返回顶部 返回列表