qt-mvvm代码分析

40 篇文章 136 订阅

qt-mvvm是GitHub上一套基于Qt的MVVM框架。本文旨在通过深入分析qt-mvvm的实现过程,来研究MVVM的实现要点。

一、概念、原理

Ref. from Martin Fowler's GUI Architecture

The controls display data - in this case about the reading. This data will pretty much always come from somewhere else, in this case let's assume a SQL database as that's the environment that most of these client-server tools assume. In most situations there are three copies of the data involved:

  • One copy of data lies in the database itself. This copy is the lasting record of the data, so I call it the record state. The record state is usually shared and visible to multiple people via various mechanisms.
  • A further copy lies inside in-memory Record Sets within the application. Most client-server environments provided tools which made this easy to do. This data was only relevant for one particular session between the application and the database, so I call it session state. Essentially this provides a temporary local version of the data that the user works on until they save, or commit it, back to the database - at which point it merges with the record state. I won't worry about the issues around coordinating record state and session state here: I did go into various techniques in [P of EAA].
  • The final copy lies inside the GUI components themselves. This, strictly, is the data they see on the screen, hence I call it the screen state. It is important to the UI how screen state and session state are kept synchronized.

Keeping screen state and session state synchronized is an important task. A tool that helped make this easier was Data Binding. The idea was that any change to either the control data, or the underlying record set was immediately propagated to the other. So if I alter the actual reading on the screen, the text field control effectively updates the correct column in the underlying record set.

In general data binding gets tricky because if you have to avoid cycles where a change to the control, changes the record set, which updates the control, which updates the record set.... The flow of usage helps avoid these - we load from the session state to the screen when the screen is opened, after that any changes to the screen state propagate back to the session state. It's unusual for the session state to be updated directly once the screen is up. As a result data binding might not be entirely bi-directional - just confined to initial upload and then propagating changes from the controls to the session state.

There are various ways of getting this kind of thing to work - the common one for client-server toolkits was the notion of events. Each control had a list of events it could raise. Any external object could tell a control that it was interested in an event - in which case the control would call that external object when the event was raised. Essentially this is just a rephrasing of the Observer pattern where the form is observing the control. The framework usually provided some mechanism where the developer of the form could write code in a subroutine that would be invoked when the event occurred. Exactly how the link was made between event and routine varied between platform and is unimportant for this discussion - the point is that some mechanism existed to make it happen.

Ref. from Martin Fowler's Data Bingding

A mechanism that ensures that any change made to the data in a UI control is automatically carried over to the underlying session state (and vice versa).

网络资料

qt-mvvmhttps://github.com/gpospelov/qt-mvvm

Martin Fowler: GUI Architectureshttps://martinfowler.com/eaaDev/uiArchs.html

Qt MVVM(Model-View-ViewModel)是一种基于Qt框架的设计模式,用于构建用户界面(UI)和业务逻辑的解耦合的应用程序。它是基于传统的MVC(Model-View-Controller)模式的进一步演化。 在Qt MVVM中,Model负责数据的管理和操作,它提供了底层数据对象和数据操作的接口。View是用户界面的呈现,它通过绑定机制和ViewModel交互,以显示数据和响应用户的操作。ViewModel是连接Model和View的桥梁,它将Model的数据转化为View所需的格式,并将用户的输入和操作转化为Model所需的格式。ViewModel还提供了数据绑定机制,以确保Model和View之间的同步。 通过使用Qt MVVM,我们可以实现以下优点: 1. 解耦合:通过将业务逻辑与用户界面解耦合,使得代码易于维护和测试。ViewModel充当了Model和View之间的中间层,使得它们可以独立地进行修改和调整。 2. 可维护性:由于代码的解耦合,使得我们可以更容易地对应用程序进行修改和更新。当需要改变用户界面或业务逻辑时,只需要修改ViewModel或Model的代码,而不会影响其他部分。 3. 可测试性:MVVM模式提供了更好的测试性,我们可以更容易地编写单元测试,以验证Model和ViewModel的功能是否正常工作。这使得我们可以更好地管理和保证代码的质量。 4. 数据绑定:Qt MVVM提供了高效的数据绑定机制,使得我们可以非常简便地将数据与用户界面关联起来。这大大减少了手动更新UI的工作量,提高了开发效率。 总之,Qt MVVM是一种强大的设计模式,可以帮助我们构建易于维护和测试的应用程序。通过解耦合和数据绑定,它提供了一种更合理和高效的方法来管理用户界面和业务逻辑之间的交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值