QTableView QStandardItemModel还原或撤消用户条目 [英] QTableView QStandardItemModel revert or undo user entry

查看:1156
本文介绍了QTableView QStandardItemModel还原或撤消用户条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何撤消或还原QStable项目模型弹出的QTableView上的用户条目?

How can I undo or revert an user entry on a QTableView popuplated by QStandarItemModel?

我已将dataChanged信号与处理程序连接,连接(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(validateData(QModelIndex,QModelIndex)))

I have connected dataChanged signal with a handler where I validate the data...

connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(validateData(QModelIndex,QModelIndex)));

...但是如果用户输入错误,我想还原或撤消用户条目

...but in case user entry is wrong I want to revert or undo the user entry to the previous value of the item.

我已经阅读关于revert()成员继承自QAbstractItemModel,但我不能理解它是如何工作的。文档说让模型知道它应该丢弃缓存的信息。但我不确定用户输入的数据是否已缓存或已存储在模型上。

I have read about revert() member inherited from QAbstractItemModel but I can't understand how it works exactly. Documentation says "Lets the model know that it should discard cached information." but I'm not sure whether the data the user entered is cached or is already stored on the model.

无论如何,如果我尝试...

Anyway if I try...

model->revert();

...在错误的用户输入后,它不起作用。

...after a wrong user entry it does not work.

提前感谢!

推荐答案

检查Qt的 undo framework 。文档中的介绍说:

Check out Qt's undo framework. The introduction at the documentation says:


Qt的撤消框架是命令模式
用于在应用程序中实现撤消/重做功能。

Qt's Undo Framework is an implementation of the Command Pattern, for implementing undo/redo functionality in applications.

命令模式基于这样的想法:
应用程序中的所有编辑都是通过创建命令对象的实例来完成的。命令
对象将更改应用于文档,并存储在命令
stack上。此外,每个命令知道如何撤销对
的更改,使文档回到其先前的状态。只要
应用程序只使用命令对象来改变
文档的状态,就可以通过向下遍历
栈来撤销一系列命令,并调用每个命令的undo转。
也可以通过向上遍历堆栈
并在每个命令上调用redo来重做一系列命令。

The Command pattern is based on the idea that all editing in an application is done by creating instances of command objects. Command objects apply changes to the document and are stored on a command stack. Furthermore, each command knows how to undo its changes to bring the document back to its previous state. As long as the application only uses command objects to change the state of the document, it is possible to undo a sequence of commands by traversing the stack downwards and calling undo on each command in turn. It is also possible to redo a sequence of commands by traversing the stack upwards and calling redo on each command.

这篇关于QTableView QStandardItemModel还原或撤消用户条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆