Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Any simple example Qt Undo Framework?
Forum Updated to NodeBB v4.3 + New Features

Any simple example Qt Undo Framework?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 312 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexey Serebryakov
    wrote on last edited by Alexey Serebryakov
    #1

    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!

    Chris KawaC 1 Reply Last reply
    0
    • A Alexey Serebryakov

      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!

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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();
      }
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved