【问题标题】:QT Graphic scene/view - moving around with mouseQT 图形场景/视图 - 用鼠标移动
【发布时间】:2016-03-08 10:41:06
【问题描述】:

我创建了自己的类(视图和场景)来显示我添加到其中的图像和对象,甚至在我的视图中实现了放大/缩小功能,但现在我必须添加新功能,我什至不知道如何开始寻找它。

  • 每当我按下鼠标的滚动按钮并按住它时 - 我希望在场景中移动,查看它的不同部分 - 就像我使用滑块一样。它应该类似于任何其他允许放大/缩小图像并在缩放后的图片周围移动以查看其不同部分的程序。

不幸的是 - 我什至不知道如何寻找一些基本的东西,因为“移动”和类似的东西是指拖动物体。

编辑 1

void CustomGraphicView::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::MidButton)
    {
        setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
        translate(event->x(),event->y());
    }
}

试过这个 - 但它正在反向工作。

【问题讨论】:

标签: c++ qt qgraphicsview qgraphicsscene


【解决方案1】:

我想你知道如何使用 Qt 处理事件。

因此,要翻译(移动)您的视图,请使用QGraphicsView::translate() 方法。

编辑

使用方法:

void CustomGraphicsView::mousePressEvent(QMouseEvent* event)
{
    if (e->button() == Qt::MiddleButton)
    {
        // Store original position.
        m_originX = event->x();
        m_originY = event->y();
    }
}

void CustomGraphicsView::mouseMoveEvent(QMouseEvent* event)
{
    if (e->buttons() & Qt::MidButton)
    {
        QPointF oldp = mapToScene(m_originX, m_originY);
        QPointF newP = mapToScene(event->pos());
        QPointF translation = newp - oldp;

        translate(translation.x(), translation.y());

        m_originX = event->x();
        m_originY = event->y();
    }
}

【讨论】:

  • 是的,我知道。所以我必须在我的 GraphicView 中使用 mouseEven 并调用该函数,是吗?
  • 我尝试了 -> 编辑 1
  • 我尝试了您的代码 - 与我的尝试类似,但仍然出现相同的错误 - 以与鼠标相反的方向查看移动
  • x 和 y 方向都相反?
  • 就我而言,还需要setTransformationAnchor(QGraphicsView::NoAnchor) 才能使事情正常进行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2018-06-09
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多