0

I am having a QgraphicsView which contains multiple QGraphicsItem. I have some features like zoom-in, zoom-out, Hide - Unhide selected item, Undo-Redo.
To implement Undo-Redo feature, I am using Qt's Undo framework ( Command Pattern).
I want to separate out Undo-Redo feature of zoom-in/out with Undo-Redo feature of Hide/Unhide.
In a nutshell, I do not want to use same stack for all Undo-Redo feature. Separate stack for zoom-in/out and separate stack for Hide-Unhide.

Is separate stack possible ? If yes, then how to do that ?

Below Undo-Redo code is of Zoom-in/out and it just for reference purpose.

myCommand.h

class myCommand: public QUndoCommand
{
public:
    myCommand();
    myCommand(double scale, QGraphicsScene* scene,QGraphicsView* view);    
private:
       QGraphicsItem* mItem;
       QGraphicsScene* mScene;
       QGraphicsView* mView;
       double scaleFactor;
       void undo();
       void redo();  
}

myCommand.cpp

myCommand::myCommand(double scale, QGraphicsScene *scene,QGraphicsView* view): mScene(scene),
        mView(view),scaleFactor(scale)
    {}
void guiCommand::undo()
 {
      mView->scale(1/scaleFactor,1/scaleFactor);
 }

void myCommand::redo()
{
     mView->scale(scaleFactor,scaleFactor);
}

myView.cpp

 void myView::ZoomIn()
    {
        double scaleFactor = 1.1;
        myCommand* command1 = new myCommand(scaleFactor,scene,view);
        undoStack->push(command1);
        view->scale(scaleFactor,scaleFactor);
    }

   void myView::ZoomOut()
   {
      double scaleFactor = 1.1;
      myCommand* command2 = new myCommand(1/scaleFactor,_scene,_view);
      undoStack->push(command2 );
      view->scale(1/scaleFactor,1/scaleFactor);
   }

myView.h

public:
 QUndoStack* undoStack;  
tushar
  • 313
  • 4
  • 10
  • It should be possible but would have manage which operation is done on which stack yourself, and you must ensure that the operations can also be undone, if the other stack has changed the state in between. – gerum May 05 '22 at 07:44
  • @gerum Can you tell me, how to implement multiple stack by taking reference of above code ? – tushar May 05 '22 at 08:33
  • 4
    by having to undoStack variables. QUndoStack* undoStack1; QUndoStack* undoStack2; And then push the commands to the correct stack. – gerum May 05 '22 at 10:55
  • @gerum Your suggestion solved my problem. Thank you. – tushar May 09 '22 at 09:08
  • Note for future readers: Do NOT copy this code (without modifications). The implementation of this undo/redo feature is flavoured. See [Undo-Redo functionality using Command-Pattern in Qt for FitInView feature](https://stackoverflow.com/questions/72159106/undo-redo-functionality-using-command-pattern-in-qt-for-fitinview-feature/72161238) for the correct usage of the Command-Pattern used by Qt's undo framework. – m7913d May 09 '22 at 10:17

0 Answers0