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. Pop the current undo command (being run) in an undo stack
Forum Updated to NodeBB v4.3 + New Features

Pop the current undo command (being run) in an undo stack

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 732 Views
  • 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.
  • M Offline
    M Offline
    m3g1dd
    wrote on last edited by
    #1

    I'm calling an undo command from within QML:

    cppClass.undoHandler.createCommand(option)
    

    The C++ undo-handler pushes undo command into undo stack:

    void UndoHandler::createCommand(
            const QString & option
            )
    {
        m_undoStack->push(new Command(
                              option
                              ));
    }
    

    The actual C++ undo command is:

    class Command : public QUndoCommand
    {
        // ...
        
        virtual void undo();
        virtual void redo();
    
        // ...
    
        Logic *m_logic;
    }
    
    void Command::redo()
    {
        m_outputName = m_logic->run(m_option);
    }
    

    And the logic runs like this:

    QString Logic::run(const QString option)
    {
        if ( /*  some condition here */ ) {
            
            // ** What I want to achieve:
            // If this condition is met
            // abort the current undo-command
            // I mean pop the current undo-command which is already pushed into stack
            
        }
    }
    

    How can I abort/pop/break the undo-command by the above condition inside the logic? I'm not sure how it should be designed, any idea?

    JonBJ 1 Reply Last reply
    0
    • M m3g1dd

      I'm calling an undo command from within QML:

      cppClass.undoHandler.createCommand(option)
      

      The C++ undo-handler pushes undo command into undo stack:

      void UndoHandler::createCommand(
              const QString & option
              )
      {
          m_undoStack->push(new Command(
                                option
                                ));
      }
      

      The actual C++ undo command is:

      class Command : public QUndoCommand
      {
          // ...
          
          virtual void undo();
          virtual void redo();
      
          // ...
      
          Logic *m_logic;
      }
      
      void Command::redo()
      {
          m_outputName = m_logic->run(m_option);
      }
      

      And the logic runs like this:

      QString Logic::run(const QString option)
      {
          if ( /*  some condition here */ ) {
              
              // ** What I want to achieve:
              // If this condition is met
              // abort the current undo-command
              // I mean pop the current undo-command which is already pushed into stack
              
          }
      }
      

      How can I abort/pop/break the undo-command by the above condition inside the logic? I'm not sure how it should be designed, any idea?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @m3g1dd
      The simple answer is (supposed to be) "you can't"! Qt deliberately does not allow you an interface for this. Once you put an undo record on the stack you cannot access/pop/change it. Blame them, not me :)

      You could have a look at https://forum.qt.io/topic/72978/how-to-remove-last-step-from-qundostack/5. Confirmation (but no suggestion) in https://stackoverflow.com/questions/44971604/qundostack-remove-specific-command/44972136.

      M 2 Replies Last reply
      1
      • JonBJ JonB

        @m3g1dd
        The simple answer is (supposed to be) "you can't"! Qt deliberately does not allow you an interface for this. Once you put an undo record on the stack you cannot access/pop/change it. Blame them, not me :)

        You could have a look at https://forum.qt.io/topic/72978/how-to-remove-last-step-from-qundostack/5. Confirmation (but no suggestion) in https://stackoverflow.com/questions/44971604/qundostack-remove-specific-command/44972136.

        M Offline
        M Offline
        m3g1dd
        wrote on last edited by
        #3

        @JonB I'm going to use if condition just before pushing undo-command into undo-stack.

        1 Reply Last reply
        0
        • JonBJ JonB

          @m3g1dd
          The simple answer is (supposed to be) "you can't"! Qt deliberately does not allow you an interface for this. Once you put an undo record on the stack you cannot access/pop/change it. Blame them, not me :)

          You could have a look at https://forum.qt.io/topic/72978/how-to-remove-last-step-from-qundostack/5. Confirmation (but no suggestion) in https://stackoverflow.com/questions/44971604/qundostack-remove-specific-command/44972136.

          M Offline
          M Offline
          m3g1dd
          wrote on last edited by
          #4

          @JonB

          I used the if condition before pushing undo command into stack like this:

          void UndoHandler::createCommand(
                  const QString & option
                  )
          {
              Command *command = new Command(option);
          
              // Check validity before pushing undo command into undo stack
              bool isValid = command->doubleCheck();
          
              if (isValid) {
                  m_undoStack->push(command);
              } else {
                  delete command;
              }
          }
          

          Undo command has a new method to check the if condition:

          class Command : public QUndoCommand
          {
              // ...
              
              virtual void undo();
              virtual void redo();
          
              // ...
          
              // New method to check validity
              bool doubleCheck();
          
              // ...
          
              Logic *m_logic;
              const QString m_option;
          }
          
          bool Command::doubleCheck() {
              return m_logic->doubleCheck(m_option);
          }
          
          void Command::redo()
          {
              // Redo method has to be modified accordingly
          }
          

          The if condition is inside a separate method in logic:

          bool Logic::doubleCheck(const QString option)
          {
              if ( /* some condition here */ )
                  return true;
              else
                  return false;
          }
          
          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