Any simple example Qt Undo Framework?
-
Hi guys,
i've tried to apply Qt Undo Framwork in my widgets application. So that is VERY terrible. Who develop that framework? I cannot understand how it works, that is out of my mind.
Could any provide me VERY simple example how it works? For example, we have QLineEdit and two buttons undo and redo.
I've already broke my brain! Spent 3 weeks for this task but nothing.
Thanks a lot!
-
Hi guys,
i've tried to apply Qt Undo Framwork in my widgets application. So that is VERY terrible. Who develop that framework? I cannot understand how it works, that is out of my mind.
Could any provide me VERY simple example how it works? For example, we have QLineEdit and two buttons undo and redo.
I've already broke my brain! Spent 3 weeks for this task but nothing.
Thanks a lot!
@Alexey-Serebryakov QLineEdit already uses its own internal undo stack, so it's not a very good class to show how to use the undo classes, but here you go:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QLineEdit* lineEdit = new QLineEdit(); QPushButton* undoButton = new QPushButton("Undo"); QPushButton* redoButton = new QPushButton("Redo"); QObject::connect(undoButton, &QPushButton::clicked, lineEdit, &QLineEdit::undo); QObject::connect(redoButton, &QPushButton::clicked, lineEdit, &QLineEdit::redo); QWidget widget; new QVBoxLayout(&widget); widget.layout()->addWidget(lineEdit); widget.layout()->addWidget(undoButton); widget.layout()->addWidget(redoButton); widget.show(); return a.exec(); }
To implement your own undo stack you just need to create an undo command for every action. For example here's a command that changes widget's window title:
class MyCommand : public QUndoCommand { public: MyCommand(QWidget* w, const QString& newText) : widget(w), undoText(w->windowTitle()), redoText(newText) {} void undo() override { widget->setWindowTitle(undoText); } void redo() override { widget->setWindowTitle(redoText); } private: QWidget* widget {}; QString undoText; QString redoText; };
Now when you perform an action you simply add a command to the stack (which calls redo) and then you can call undo and redo on the stack:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget widget; QUndoStack stack; QPushButton* doSomethingButton = new QPushButton("Do something"); QPushButton* undoButton = new QPushButton("Undo"); QPushButton* redoButton = new QPushButton("Redo"); QObject::connect(undoButton, &QPushButton::clicked, &stack, &QUndoStack::undo); QObject::connect(redoButton, &QPushButton::clicked, &stack, &QUndoStack::redo); QObject::connect(doSomethingButton, &QPushButton::clicked, &widget, [&]{ static int foo = 1; stack.push(new MyCommand(&widget, "Hello!" + QString::number(foo++))); }); new QVBoxLayout(&widget); widget.layout()->addWidget(doSomethingButton); widget.layout()->addWidget(undoButton); widget.layout()->addWidget(redoButton); widget.show(); return a.exec(); }
-
Hi,
Did you also take a look at the offical example ?
It shows in a pretty nice way how to implement support for multiple commands.