c++ - QTableView QStandardItemModel 恢复或撤消用户输入

标签 c++ qt qabstractitemmodel qstandarditemmodel

如何撤消或恢复由 QStandarItemModel 弹出的 QTableView 上的用户条目?

我已将 dataChanged 信号连接到一个处理程序,我在其中验证数据...

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

...但如果用户输入错误,我想将用户输入恢复或撤消为项目的先前值。

我读过有关从 QAbstractItemModel 继承的 revert() 成员的信息,但我不明白它是如何工作的。文档说“让模型知道它应该丢弃缓存的信息。”但我不确定用户输入的数据是否已缓存或已存储在模型中。

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

model->revert();

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

提前致谢!

最佳答案

查看 Qt 的 undo framework .文档中的介绍说:

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

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.

关于c++ - QTableView QStandardItemModel 恢复或撤消用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9899735/

相关文章:

c++ - 非 ASCII 字符 - 从 std::string 到 char* 的转换

c++ - g++ 在多个文件上比使用 Google mock 的单一文件慢得多

c++ - 如何使用 boost lambda 在集合中的每个元素上调用方法?

c++ - 如何在我的 Qt 应用程序中存储密码?

c++ - 当我已经初始化了指针属性时,如何将 QGraphicsItem 向下转换为创建的类?

c++ - QAbstractItemModel 和 QTreeView

c++ - 在类矩阵 mat(m,n) 中初始化 vector<vector<int>> data(m, vector<int>(n))

c++ - 是否可以只使用 QT 和 WebAssembly(而不是 HTML + CSS + JavaScript)来开发前端 Web?

python - QAbstractItemModel 和 QModelIndex 之间的相互作用

c++ - QAbstractItemModel data() 永远不会被调用