Search code examples
pythonqtscrollbarqtableviewpyqt6

PyQT6 make verticalScrollBar() QWheelEvent scroll 1 row instead of 3


I have a QTableView with a verticalScrollBar()

The problem is that when I use the mouse wheel to scroll down/up it moves 3 rows at a time, I'd like to change this to only move 1 row at a time.

Looking at the class reference it seems to implement QWheelEvent,so my best guess is I must overwrite this event...however I have absolutely no idea where to start.

Any help would be much appreciated!

I'm using Python 3.10.5 and Pyqt 6.3 on Arch Linux (Manjaro)


Solution

  • The QApplication class has the setWheelScrollLines method for setting this property globally. But if you only want it to affect one widget, you can create a subclass and utilise it in a reimplemented wheelEvent.

    Here's a simple demo:

    from PyQt6 import QtGui, QtWidgets
    
    class TableView(QtWidgets.QTableView):
        def __init__(self):
            super().__init__()
            model = QtGui.QStandardItemModel(self)
            for row in range(100):
                model.appendRow(QtGui.QStandardItem())
            self.setModel(model)
    
        def wheelEvent(self, event):
            lines = QtWidgets.QApplication.wheelScrollLines()
            try:
                QtWidgets.QApplication.setWheelScrollLines(1)
                super().wheelEvent(event)
            finally:
                QtWidgets.QApplication.setWheelScrollLines(lines)
        
    app = QtWidgets.QApplication(['Test'])
    window = TableView()
    window.show()
    app.exec()