[PyQt] Horizontal scroll on wheelEvent with Shift too fast

Colin Huang colin.huang at cae.com
Mon Sep 19 17:40:57 BST 2016


I have 200 objects on the MyView that is derived from the QGraphicsView. I use the mouse wheel to scroll vertically, everything is nice and smooth. I think that is the Qt default implementation.

I implement the Shift + wheel to scroll horizontally, and the horizontal scroll bar jumps a huge step on a single wheel event - 10 times bigger than that on the vertical scroll bar.

Basically, the same wheel event drives the horizontal scroll bar 10 times farther than the vertical scroll bar.

How to slow down the horizontal scroll bar?

Well, I tried to reduce the original event's angleDelta value like this event.angleDelta()/10 to generate a new wheel event and use the new event to drive the horizontal scroll bar. That works but seems to be too complicated.

I also notice it is the Shift modifier that affects the horizontal scroll bar behavior. If I do self.horizontalScrollBar().wheelEvent(event) without the Shift modifier, the horizontal bar scrolling is 10 times slower. That is weird. Why does pressing Shift change the horizontal scrolling behavior?!

Please help. Here is the self-contained program:

#----------------------
import sys
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsTextItem, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QWheelEvent


class MyView(QGraphicsView):
    def wheelEvent(self, event: QWheelEvent):
        if event.modifiers() == Qt.ShiftModifier:
            self.horizontalScrollBar().wheelEvent(event)
        else:
            self.verticalScrollBar().wheelEvent(event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    scene = QGraphicsScene()
    view = MyView(scene)

    # Generate 200 objects, 100 on x and 100 on y.
    for i in range(100):
        obj_on_x = QGraphicsTextItem("X" + str(i))
        obj_on_x.setX(i * 100)
        scene.addItem(obj_on_x)

        obj_on_y = QGraphicsTextItem("Y" + str(i))
        obj_on_y.setY(i * 100)
        scene.addItem(obj_on_y)

    view.show()
    sys.exit(app.exec_())


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160919/5ae05cc2/attachment.html>


More information about the PyQt mailing list