2016-03-08 44 views
0

我有一个应用程序,可以在Qt Creator中使用鼠标滚轮来处理缩放。在Qt中按下鼠标滚轮时的平移C++

CPP

void QNodeView::wheelEvent(QWheelEvent* event) { 

    setTransformationAnchor(QGraphicsView::AnchorUnderMouse); 

    // Scale the view/do the zoom 
    double scaleFactor = 1.15; 
    if(event->delta() > 0) { 
     // Zoom in 
     scale(scaleFactor, scaleFactor); 
    } else { 
     // Zooming out 
     scale(1.0/scaleFactor, 1.0/scaleFactor); 
    } 
} 

这是

protected: 
    //Take over the interaction 
    virtual void wheelEvent(QWheelEvent* event); 

如何添加鼠标中键可进行平移的能力,与被按下的用户拖动头文件 ^h光标?

如果需要的话,我可以发布项目代码。 感谢

项目文件的链接(Qt Creator的项目)

https://www.dropbox.com/s/gbt4qqtdedltxek/QNodesEditor-master_01.zip?dl=0

+1

覆盖mousePressEvent(),mouseMoveEvent(),和mouseReleaseEvent(),和内部的每个那些试验的事件 - >按钮()== Qt的:: MiddleButton –

回答

0

起初,引入一些新的成员变量到您的浏览器类:

class QNodeView : public QGraphicsView 
{ 
    // ... 

private: 
    int m_originalX = 0; 
    int m_originalY = 0; 
    bool m_moving = false; 
}; 

然后重新实现mousePressEvent()mouseMoveEvent()mouseReleaseEvent()

void QNodeView::mousePressEvent(QMouseEvent* event) 
{ 
    if (event->button() == Qt::MiddleButton) 
    { 
     // store original position 
     m_originalX = event->x(); 
     m_originalY = event->y(); 

     // set the "moving" state 
     m_moving = true; 
    } 
} 

void QNodeView::mouseMoveEvent(QMouseEvent* event) 
{ 
    if (m_moving) 
    { 
     // panning operates in the scene coordinates using x,y 
     QPointF oldp = mapToScene(m_originalX, m_originalY); 
     QPointF newp = mapToScene(event->pos()); 
     QPointF translation = newp - oldp; 

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

     m_originalX = event->x(); 
     m_originalY = event->y(); 
    } 
} 

void QNodeView::mouseReleaseEvent(QMouseEvent* event) 
{ 
    if (event->button() == Qt::MiddleButton) 
    { 
     m_moving = false; 
    } 
} 
+0

我试图我的样品中,它没有似乎移动了这个观点呢?我应该发布代码 – JokerMartini

+0

是的,发布代码请 – Tomas

+0

我已经添加到OP中的收件箱文件的链接,我真的很感谢你对此的帮助。该项目来自公共github存储库。 – JokerMartini