Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?



  • @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.



  • @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.



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



  • @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;
    }
    

Log in to reply