Question

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

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.

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.

Thanks in advance!

Was it helpful?

Solution

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top