Problem with resetting scrollbars when left mouse press event is sent to QGraphicsView

56 Views Asked by At

So, I am currently trying to implement dragging mode on mouse with ScrollHandDrag everytime user presses middle button on QGraphicsView. The problem is - to actually drag with scroll hand drag mode you have to left click.

My solution for that was to simply override MousePressEvent method in a class that inherits after QGraphicsScene and catch that middle mouse button event here, access QGraphicsView that uses this QGraphicsScene, set ScrollHandDrag drag mode on the QGraphicsView and send one more event to QGraphicsView from QGraphicsScene that would be a left mouse click. This way someone could enable ScrollHandDrag with only middle mouse button, and actually drag with just that button.

This is essentially done like this:

case Qt::MiddleButton: {
    views()[0]->setDragMode(QGraphicsView::ScrollHandDrag);

    QMouseEvent eventGraphicsView(QEvent::MouseButtonPress, event->pos(), event->scenePos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QApplication::sendEvent(views()[0]->viewport(), &eventGraphicsView);
    break;
}

MousePressEvent had to be overriden in class that inherits after QGraphicsScene since QGraphicsView isn't available considering how the code is being structured now.

The problem here is, even though it works properly and I can drag on QGraphicsView - everytime I try to drag, scrollbars reset their position to whatever it was before. It seems like the view jumps to last position with it's scrollbars.

So the question is - why is that? Should the event be different? When I implemented this with for example ctrl + left click usage, everything works fine, although in this implementation I don't send any type of event to QGraphicsView, so I guess the issue lies somewhere here.

0

There are 0 best solutions below