如何在QT中获取Qgraphicsview的当前可见部分?

发布于 2025-01-17 12:20:10 字数 1528 浏览 3 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2025-01-24 12:20:10

正如 mapToScene 的源代码所示,三个参数定义了场景中在视口上可见的部分:

要实现撤消重做框架,这三个参数应在每次操作之前存储并在撤消时恢复。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文