如何在QT中获取Qgraphicsview的当前可见部分?
我有 QGraphicsView,它有多个 QGraphicsItem。在此视图上,我正在使用诸如放大、缩小、适合等功能来应用一些转换。
因此,在对当前视图应用 fit-in
功能之前,我想将视图的当前转换(视图的当前情况)存储在变量中。然后我想应用Fit-in
功能。 (这样,在撤消重做中我可以使用该存储的位置)
但我不知道如何将视图的当前位置存储在变量中。我尝试了这种方式:
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();
}
如何将当前视图存储在另一个变量中?
***** 编辑 ******
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如
mapToScene 的源代码所示
,三个参数定义了场景中在视口上可见的部分:QGraphicsView::transform()
QAbstractScrollArea::horizontalScrollBar()->value()
QAbstractScrollArea::verticalScrollBar()->value()
要实现撤消重做框架,这三个参数应在每次操作之前存储并在撤消时恢复。
As revealed by source code of
mapToScene
, three parameters define the part of the scene that is visible on the viewport:QGraphicsView::transform()
QAbstractScrollArea::horizontalScrollBar()->value()
QAbstractScrollArea::verticalScrollBar()->value()
To implement an undo-redo framework, those three parameters should be stored before every operation an restored upon undo.