Qt: the correct method to perform panning (drag and drop)

QGraphicsview has a setDragMode(ScrollHandDrag) method to enable left-click panning. But I wanted to enable panning when I click the mouse wheel (middle button) and created the following custom implementation for panning:

 //Within a custom derived class of QGraphicsView //pan is true when wheel is clicked and false when released //last Pos is defined somewhere else in the class void GridView::mouseMoveEvent(QMouseEvent *event){ if(pan){ QPointF currentPos = event->pos(); QPointF relPos = currentPos - lastPos; lastPos = currentPos; //this is what creates the panning effect translate(relPos.x(), relPos.y()); } QGraphicsView::mouseMoveEvent(event); } 

This works fine for the most part. But, for example, if I scale the identity matrix to 1,000,000, this method will fail and stop panning (as if the view was stuck). This problem does not occur if I use setDragMode()

What would be the correct custom implementation of setDragMode() to turn it on with the click of a wheel?

+4
source share
1 answer

This works for me ... these are Qt tricks, thinking that the user presses the left mouse button when he really presses the middle one.

 void GridView :: mousePressEvent(QMouseEvent * e) { if (e->button() == MidButton) { QMouseEvent fake(e->type(), e->pos(), LeftButton, LeftButton, e->modifiers()); QGraphicsView::mousePressEvent(&fake); } else QGraphicsView::mousePressEvent(e); } void GridView :: mouseReleaseEvent(QMouseEvent * e) { if (e->button() == MidButton) { QMouseEvent fake(e->type(), e->pos(), LeftButton, LeftButton, e->modifiers()); QGraphicsView::mouseReleaseEvent(&fake); } else QGraphicsView::mouseReleaseEvent(e); } 
+10
source

Source: https://habr.com/ru/post/1499952/


All Articles