Search code examples
c++qtundo-redo

Explicitly calling redo() under the Qt Undo Framework


I am using the Qt Undo Framework (http://qt-project.org/doc/qt-4.8/qundo.html), and I want to explicitly call the undo() and redo() on the QUndoStack. I looked up the Qt docs and searched to see if this was supported, but couldn't find anything. So, I went ahead and did it.

Results - Explicit call to undo() works fine. canRedo() returns true thereafter. A subsequent call to redo() does not enter any of the redo() functions I have defined.

Please provide some insights into this. Please let me know if I need to share more information. Thanks!


Solution

  • Seems to work as expected and documented. Note that pushing a command to the stack will always call redo() on the command object.

    class DummyCmd : public QUndoCommand
    {
    public:
        DummyCmd()
            : QUndoCommand(){ qDebug() << "DummyCmd c-tor"; }
        virtual void undo()
            { qDebug() << "undo"; }
        virtual void redo()
            { qDebug() << "redo"; }
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        DummyCmd c1;
        QUndoStack s;
        qDebug() << "pushing to stack";
        s.push(&c1);
        qDebug() << "calling undo";
        s.undo();
        qDebug() << "canRedo after undo: " << s.canRedo();
        s.redo();
        qDebug() << "canRedo after redo: " << s.canRedo();
    
        return a.exec();
    }
    

    Output:

    DummyCmd c-tor
    pushing to stack
    redo
    calling undo
    undo
    canRedo after undo:  true
    redo
    canRedo after redo:  false