As I have some additional constraints on it, I am trying to implement custom scrolling on a QGraphicsView. My implementation used to work on Windows, but on X11, I got some problems even for very basic scrolling.

When I catch the mouse-press-event or mouse-move-event, I store the event. On the next move then, I calculate a delta between those mouse positions, and set the values of the horizontal and vertical scroll bars accordingly.

The problem now is that when a scrollbar value changes, I get another mouse move event (as a result of the graphics view moving under the mouse), which is just inverse to the delta that I put onto the scrollbars. So as a result, no scrolling is done (except some flicker).

The following implementation of a mouse move event will result two "phantom" mouse move events for each "real" event (user actually moves the mouse). Those phantom events come from the two QAbstractSlider::setValue() calls.

Qt Code:
  1. void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
  2. {
  3. QScrollBar *hBar = horizontalScrollBar();
  4. QScrollBar *vBar = verticalScrollBar();
  5. QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
  6. ensureSceneFillsView();
  8. hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
  9. vBar->setValue(vBar->value() - delta.y());
  10. storeMouseEvent(event);
  11. lastMouseEvent.setAccepted(false);
  12. event->accept();
  13. }
To copy to clipboard, switch view to plain text mode 

How do I get scrolling to work properly?