Home > database >  How to get current visible portion of QGraphicsView in Qt?
How to get current visible portion of QGraphicsView in Qt?

Time:03-29

I have QGraphicsView, which has multiple QGraphicsItem's. On this view, I am applying some transformation using features like Zoom-in, zoom-out, fit -In etc.
So before applying fit-in feature on my current view, I want to store view's current transformation ( view's current situation ) in a variable. And then I want to apply Fit-in feature. ( so that, in Undo-Redo I can use that stored position)
But I do not know how to store current position of view in a variable. I tried this way :

void myClass::FitIn()
{
     QTransform t = _view->transform();
     QScrollBar* hrSBar = _view->horizontalScrollBar();
     QScrollBar* verSBar = _view->verticalScrollBar();

     myCommand* c = new myCommand(t,hrSBar,verSBar);
     undoStack->push(c);
     _view->resetTransform();
}       
           

How to store, current view in another variable ?

***** EDIT ******

myCommand.cpp          
myCommand::myCommand(QTransform t, QScrollBar *hr, QScrollBar *vr)
{
    this->trans = t;
    this->horizontalSBar = hr;
    this->verticalSBar = vr;
}

void myCommand::undo()
{
    _mView->setTransform(trans);
    _mView->horizontalScrollBar()->setValue(horizontalSBar->maximum());
    _mView->verticalScrollBar()->setValue(verticalSBar->maximum());
}

void myCommand::redo()
{
    _mView->setTransform(trans);  // Segmentation Fault occurs
    _mView->horizontalScrollBar()->setValue(horizontalSBar->maximum());
    _mView->verticalScrollBar()->setValue(verticalSBar->maximum());
}

myCommand.h

class myCommand: public QUndoCommand
{
public:
    myCommand(QTransform t, QScrollBar* hr, QScrollBar* vr);
private:
    QGraphicsScene* _mScene;
    QGraphicsView* _mView;
}

CodePudding user response:

As revealed by source code of mapToScene, three parameters define the part of the scene that is visible on the viewport:

To implement an undo-redo framework, those three parameters should be stored before every operation an restored upon undo.

  • Related