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. Undo actions.
Qt 6.11 is out! See what's new in the release blog

Undo actions.

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 2 Posters 11.8k Views 1 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.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by Loc888
    #3

    OK, but i need to create my own actions, because the program does custom stuff, so i need to do for example

    void Undo_Something()
    {
            Variable_1--
            Variable_2++
    }
    void Redo_Something()
    {
            Variable_1++
            Variable_2--
    }
    
    

    Or it's automatic?
    I mean, i just need to call undo() and redo(), or i need to edit it?

    mrjjM 1 Reply Last reply
    0
    • L Loc888

      OK, but i need to create my own actions, because the program does custom stuff, so i need to do for example

      void Undo_Something()
      {
              Variable_1--
              Variable_2++
      }
      void Redo_Something()
      {
              Variable_1++
              Variable_2--
      }
      
      

      Or it's automatic?
      I mean, i just need to call undo() and redo(), or i need to edit it?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      @Loc888
      Hi
      Yes you must create your own commands as the system cannot possible know what variable
      to alter when doing undo etc.

      Did you check out the examples?

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Loc888
        wrote on last edited by Loc888
        #5

        Ye, but as you can see, i am not so good with stuff like that, first time i need to do something like that. I just ask, because maybe he is "smart enough" to know it in some way.

        But ok, i will try to do that.
        So, i need to inherrit the class, implement undo() and redo(), and then create the actions?

        That "QUndoStack" class object need to be created too?

        mrjjM 1 Reply Last reply
        0
        • L Loc888

          Ye, but as you can see, i am not so good with stuff like that, first time i need to do something like that. I just ask, because maybe he is "smart enough" to know it in some way.

          But ok, i will try to do that.
          So, i need to inherrit the class, implement undo() and redo(), and then create the actions?

          That "QUndoStack" class object need to be created too?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #6

          @Loc888
          Hi
          Yes you should have a QUndoStack variable too in the class.
          Its not super complex if u take from the examples.
          Here is how a "text"command is made. ( it can insert/remove some text in a document)

          class AppendText : public QUndoCommand
          {
          public:
              AppendText(QString *doc, const QString &text)
                  : m_document(doc), m_text(text) { setText("append text"); }
              virtual void undo()
                  { m_document->chop(m_text.length()); }
              virtual void redo()
                  { m_document->append(m_text); }
          private:
              QString *m_document;
              QString m_text;
          };
          
          1 Reply Last reply
          1
          • L Offline
            L Offline
            Loc888
            wrote on last edited by
            #7

            Ok i have:

            #ifndef COMMAND_ACTIONS_H
            #define COMMAND_ACTIONS_H
            
            #include <QUndoCommand>
            #include <QUndoStack>
            
            class Command_Actions : public QUndoCommand
            {
            public:
                Command_Actions();
                virtual void redo();
                virtual void undo();
            };
            
            
            #endif // COMMAND_ACTIONS_H
            
            

            What should i put in undo() ? Because i want delete the last action, or i just do it in that QUndoStack ? I mean, delete the last action?

            #include "Command_Actions.h"
            
            Command_Actions::Command_Actions()
            {
            }
            
            void Command_Actions::redo()
            {
            
            }
            
            void Command_Actions::undo()
            {
            
            }
            
            
            mrjjM 1 Reply Last reply
            0
            • L Loc888

              Ok i have:

              #ifndef COMMAND_ACTIONS_H
              #define COMMAND_ACTIONS_H
              
              #include <QUndoCommand>
              #include <QUndoStack>
              
              class Command_Actions : public QUndoCommand
              {
              public:
                  Command_Actions();
                  virtual void redo();
                  virtual void undo();
              };
              
              
              #endif // COMMAND_ACTIONS_H
              
              

              What should i put in undo() ? Because i want delete the last action, or i just do it in that QUndoStack ? I mean, delete the last action?

              #include "Command_Actions.h"
              
              Command_Actions::Command_Actions()
              {
              }
              
              void Command_Actions::redo()
              {
              
              }
              
              void Command_Actions::undo()
              {
              
              }
              
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @Loc888
              Hi
              The undo function should revert its change.
              Like the sample the cuts the text. in your case, you just need to decrease the variable
              by one?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Loc888
                wrote on last edited by Loc888
                #9

                I think i dont undersand it.

                Maybe i explain it, so:
                I create undo() and redo() as general actions, but then i wanna be able to do more stuff.
                So it's gonna be:

                Action_Plus_5();
                Action_Minus_5();
                Action_Increment();
                Action_Decrement();
                

                And that's the problem, how i call them in undo() and redo??
                I mean, how by undo() delete the last used action, so for example Action_Plus_5() ?

                mrjjM 1 Reply Last reply
                0
                • L Loc888

                  I think i dont undersand it.

                  Maybe i explain it, so:
                  I create undo() and redo() as general actions, but then i wanna be able to do more stuff.
                  So it's gonna be:

                  Action_Plus_5();
                  Action_Minus_5();
                  Action_Increment();
                  Action_Decrement();
                  

                  And that's the problem, how i call them in undo() and redo??
                  I mean, how by undo() delete the last used action, so for example Action_Plus_5() ?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @Loc888
                  Hi
                  Please look at this
                  http://doc.qt.io/qt-5/qtwidgets-tools-undoframework-example.html
                  you can see how it uses the undoStack and how to use the system.

                  1 Reply Last reply
                  1
                  • L Offline
                    L Offline
                    Loc888
                    wrote on last edited by
                    #11

                    Ye, i see that... And i am lost.
                    I read that, and i dont know wher i must declare any functionality....

                    When i use the Stack.push(Command), how i am gonna tell to the "Command" to do something??
                    It's a variable, so how can i declare to it any functionality?
                    Add or subtract something?

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      Hi
                      You let the commands points to the data so u can change it from inside them.
                      From sample

                      class MoveCommand : public QUndoCommand
                      {
                      public:
                          enum { Id = 1234 };
                      
                          MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
                                      QUndoCommand *parent = 0);
                      
                          void undo() override;
                          void redo() override;
                          bool mergeWith(const QUndoCommand *command) override;
                          int id() const override { return Id; }
                      
                      private:
                          DiagramItem *myDiagramItem; // this would be your data. point to the variable
                          QPointF myOldPos;  // this would be old value
                      };
                      
                      and to restore you do
                      void MoveCommand::undo()
                      {
                          myDiagramItem->setPos(myOldPos); // this would be to decrease or set old value back
                          myDiagramItem->scene()->update();
                          setText(QObject::tr("Move %1")
                              .arg(createCommandString(myDiagramItem, newPos)));
                      }
                      
                      1 Reply Last reply
                      1
                      • L Offline
                        L Offline
                        Loc888
                        wrote on last edited by Loc888
                        #13

                        Ther is no way to just undo a method or funtion? I have alot of this variables, so how many of those diagrams i am gonna create?
                        It's gonna mess up the code, at this point is a lot of stuff ther.

                        Ps. It's not better, just to create one structure with all that data, and maybe then point to it?

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #14

                          Hi
                          how would u undo a method ?
                          Yes if u have a lots of single variables that need undo / redo then it will be many small classes.

                          Since i dont know your use case or the code its hard to say if there would be easier way.

                          Do you need for each variable to be able to undo many levels or just last value ?

                          Also, you can make Commands class that can handle all, you just need to specify for what data member the undo applies when undoing etc.
                          But again, there are many ways to do this but it depends on the code u already have what will be easy.

                          -Ps. It's not better, just tocreate one structure with all that data, and maybe then point to it?
                          Yes, using a struct would make is more easy to handle

                          L 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            Hi
                            how would u undo a method ?
                            Yes if u have a lots of single variables that need undo / redo then it will be many small classes.

                            Since i dont know your use case or the code its hard to say if there would be easier way.

                            Do you need for each variable to be able to undo many levels or just last value ?

                            Also, you can make Commands class that can handle all, you just need to specify for what data member the undo applies when undoing etc.
                            But again, there are many ways to do this but it depends on the code u already have what will be easy.

                            -Ps. It's not better, just tocreate one structure with all that data, and maybe then point to it?
                            Yes, using a struct would make is more easy to handle

                            L Offline
                            L Offline
                            Loc888
                            wrote on last edited by Loc888
                            #15

                            @mrjj

                            "Do you need for each variable to be able to undo many levels or just last value ?"

                            Ther almost everything must be able to undo, redo is not necessary. Needed 5 levels.
                            How to assign a structure member to that QUndoCommand object?
                            Ther is no way to point to the structure, because it's different type.
                            I dont find any method to set that equal.

                            mrjjM 1 Reply Last reply
                            0
                            • L Loc888

                              @mrjj

                              "Do you need for each variable to be able to undo many levels or just last value ?"

                              Ther almost everything must be able to undo, redo is not necessary. Needed 5 levels.
                              How to assign a structure member to that QUndoCommand object?
                              Ther is no way to point to the structure, because it's different type.
                              I dont find any method to set that equal.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @Loc888
                              Hi
                              So each variable need to be able to undo many values back ?

                              • How to assign a structure member to that QUndoCommand object?
                                You just do it. you can use any type u like.
                              class MoveCommand : public QUndoCommand
                              {
                              public:
                                  enum { Id = 1234 };
                              
                                  MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
                                              QUndoCommand *parent = 0);
                              

                              the DiagramItem and QPointF could be ANYTHIng you need
                              MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
                              those are jsut what sample need, you can just use the types u need

                              L 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @Loc888
                                Hi
                                So each variable need to be able to undo many values back ?

                                • How to assign a structure member to that QUndoCommand object?
                                  You just do it. you can use any type u like.
                                class MoveCommand : public QUndoCommand
                                {
                                public:
                                    enum { Id = 1234 };
                                
                                    MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
                                                QUndoCommand *parent = 0);
                                

                                the DiagramItem and QPointF could be ANYTHIng you need
                                MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
                                those are jsut what sample need, you can just use the types u need

                                L Offline
                                L Offline
                                Loc888
                                wrote on last edited by Loc888
                                #17

                                @mrjj said in Undo actions.:

                                diagramItem

                                At this point, i am gonna create a structure, and create 10 objects, and then simply switch them, so:
                                Memory_001 = Memory_002
                                Memory_002 = Memory_003 // And progress.

                                Is it a good idea? What about performance? (They are not so big, around 200 - 400 variables)

                                mrjjM 1 Reply Last reply
                                0
                                • L Loc888

                                  @mrjj said in Undo actions.:

                                  diagramItem

                                  At this point, i am gonna create a structure, and create 10 objects, and then simply switch them, so:
                                  Memory_001 = Memory_002
                                  Memory_002 = Memory_003 // And progress.

                                  Is it a good idea? What about performance? (They are not so big, around 200 - 400 variables)

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #18

                                  @Loc888
                                  On a desktop class pc, it would be nothing for it.
                                  But if u need multiple levels on undo such structure would not work.

                                  can you show some of the data u need multiple levels of undo for (real code) ?
                                  are they int/floats, strings etc ?
                                  Are they all of same type or very mixed?

                                  L 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Loc888
                                    On a desktop class pc, it would be nothing for it.
                                    But if u need multiple levels on undo such structure would not work.

                                    can you show some of the data u need multiple levels of undo for (real code) ?
                                    are they int/floats, strings etc ?
                                    Are they all of same type or very mixed?

                                    L Offline
                                    L Offline
                                    Loc888
                                    wrote on last edited by Loc888
                                    #19

                                    @mrjj Need at least5 of them, and max 10. Ther is rly no need to create more in this case.

                                    I show you the template:
                                    struct:Data
                                    {

                                    int*
                                    int*
                                    int*

                                    long double*
                                    long double*
                                    long double*
                                    long double*
                                    long double*
                                    long double*
                                    long double*
                                    long double*
                                    long double*

                                    };

                                    Stuff like that, i have around 4 - 6 of them, so it's like 30 objects * 10...

                                    I think is much more then 300 variables, but not all must be able to undo,
                                    cuz ther is a percentage calculator, so it's gonna re-calculate it again.
                                    Ther are around +150 variables of "percent type", so they don't need to be stored, the function gonna recalculate them by current value of variables.

                                    So i am gonna create a structure with that six structures, and the total of variables gonna be maybe 800?? (Some long double) Gonna work?

                                    mrjjM 1 Reply Last reply
                                    0
                                    • L Loc888

                                      @mrjj Need at least5 of them, and max 10. Ther is rly no need to create more in this case.

                                      I show you the template:
                                      struct:Data
                                      {

                                      int*
                                      int*
                                      int*

                                      long double*
                                      long double*
                                      long double*
                                      long double*
                                      long double*
                                      long double*
                                      long double*
                                      long double*
                                      long double*

                                      };

                                      Stuff like that, i have around 4 - 6 of them, so it's like 30 objects * 10...

                                      I think is much more then 300 variables, but not all must be able to undo,
                                      cuz ther is a percentage calculator, so it's gonna re-calculate it again.
                                      Ther are around +150 variables of "percent type", so they don't need to be stored, the function gonna recalculate them by current value of variables.

                                      So i am gonna create a structure with that six structures, and the total of variables gonna be maybe 800?? (Some long double) Gonna work?

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @Loc888
                                      Ok so those Data points to somewhere where the real data lives?
                                      And you need to be able to undo each "long double*" more than one level back?

                                      L 1 Reply Last reply
                                      0
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        I still think using the undo system ( if u need more than one level undo) is a better choice than make some sort of your own system.

                                        1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @Loc888
                                          Ok so those Data points to somewhere where the real data lives?
                                          And you need to be able to undo each "long double*" more than one level back?

                                          L Offline
                                          L Offline
                                          Loc888
                                          wrote on last edited by Loc888
                                          #22

                                          @mrjj I don't remember, i work long time on this program, but i think nothing of long double need to be stored, ther is a structure with a template for percent calculations, so around 5 object's of them, does't need to be stores (Like i said, they gonna be re-calculated).

                                          No, they are not pointers, i just put that " * " symbol, because i dont want write any names :)

                                          In real it's:

                                          struct:Data
                                          {
                                          // Names for the demonstration purposes
                                          int Name_A_Int;
                                          int Name_B_Int;
                                          int Name_C_Int;
                                          
                                          long double Name_A;
                                          long double Name_B;
                                          long double Name_C;
                                          long double Name_D;  // And progress
                                          
                                          };
                                          

                                          Ps. I am gonna try, will see what happend, i understood a little bit more, how it's gonna work, but i use it first time, and it's hard to use with a little bit bigger program.

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

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