【问题标题】:Scaling a QGrahicsView when MouseWheenTurned first scrolls, then scales当 MouseWheenTurned 第一次滚动时缩放 QGrahicsView,然后缩放
【发布时间】:2015-09-21 17:09:54
【问题描述】:

我正在尝试向我在 Qt 中绘制的图形添加放大/缩小功能。 我首先做的是用我自己的类 GraphicsScene 扩展 QGraphicsScene 并重载轮子事件。

class GraphicsScene : public QGraphicsScene
{
    Q_OBJECT
public:
    GraphicsScene(QObject *parent, bool drawAxes){ /*drawing stuff here.. */}
    virtual void wheelEvent(QGraphicsSceneWheelEvent *mouseEvent);   
signals:
    void mouseWheelTurned(int);
};


void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent* mouseEvent) {
  int numDegrees = mouseEvent->delta() / 8;
  int numSteps = numDegrees / 15; // see QWheelEvent documentation

  emit mouseWheelTurned(numSteps);
}

当车轮转动时,会向包含场景的视图发送一个事件,并执行缩放:

class GraphicsView : public QGraphicsView{
    Q_OBJECT
    qreal m_currentScale;

public:

GraphicsView(QWidget * parent): QGraphicsView(parent){  m_currentScale = 1.0; }
public slots:
    void            onMouseWheelTurned  (int);
};
void GraphicsView::onMouseWheelTurned(int steps) {
    qreal sign = steps>0?1:-1;
    qreal current = sign* pow(0.05, abs(steps));

    if(m_currentScale+current > 0){
        m_currentScale += current;
        QMatrix matrix;
        matrix.scale(m_currentScale, m_currentScale);
        this->setMatrix(matrix);

    }
}

这可行,但我注意到如果我放大很多,例如到图形的顶部,使图形不再完全在视口中,然后我缩小,程序首先滚动到底部的图形。我可以看到垂直滚动条向下滑动。只有当它到达底部时,它才会开始缩小。可能是什么问题呢?

我想在没有这种向上/向下滚动行为的情况下放大/缩小。

【问题讨论】:

  • 与其在Scene中处理这个并发送一个事件给View,为什么不直接在view中用QGraphicsView::wheelEvent处理事件,然后调用它的scale函数呢?
  • 很好的建议。谢谢。
  • 做到了!请回答原问题,我会接受你的回答!

标签: c++ qt


【解决方案1】:

最好在场景中处理这个并将事件发送到视图。

直接在视图中用QGraphicsView::wheelEvent‌​处理事件,然后调用它的scale函数。

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 2018-09-23
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多