0

I'm following a previous answer on here to allow me to scroll the content of a matplotlib axes using a PyQt5 (or Pyside2) QScrollBar.

However, I've noticed that the value of the scroll bar is not always what I would expect, regardless of what I'm using the scroll bar for.

Here's a minimal example:

import sys
from PySide2 import QtWidgets, QtCore

class MainWindow(QtWidgets.QMainWindow):
    
    def __init__(self):

        QtWidgets.QMainWindow.__init__(self)
        self.setMinimumSize(QtCore.QSize(1500, 100))

        self.scroll = QtWidgets.QScrollBar(QtCore.Qt.Horizontal)
        self.scroll.actionTriggered.connect(self.update)

        print(f"Initial value: {self.scroll.value()}")
                
        self.setCentralWidget(self.scroll)

    def update(self, event=None):
        print(self.scroll.value())
 
if __name__ == "__main__":
    
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()

This prints the initial value of the scroll bar, then each time the scroll bar is moved by the user the new value of the scroll bar is printed.

If I move the scroll bar (in single steps): Right, Left, Right, Right, Right, then the following is printed:

Initial value: 0
0
1
0
1
2

Whereas I would expect:

Initial value: 0
1
0
1
2
3

Likewise, at the right hand side of the scroll bar, the final three values printed are: 98, 99, 98, whereas I would expect the last three values to be 97, 98, 99.

I've tried both PyQt5 and PySide2 (5.15.2), but both have this behaviour.

Is there something I'm missing here?

Paul
  • 515
  • 3
  • 9
  • 1
    As the documentation about [`actionTriggered`](https://doc.qt.io/qt-5/qabstractslider.html#actionTriggered) explains: "When the signal is emitted, the `sliderPosition` has been adjusted according to the action, but the `value` has not yet been propagated". So you either use `sliderPosition()` or connect to [`valueChanged()`](https://doc.qt.io/qt-5/qabstractslider.html#valueChanged). Also, you should not overwrite [`update()`](https://doc.qt.io/qt-5/qwidget.html#update). – musicamante Feb 07 '22 at 18:01
  • Thank you! And sorry, I should have seen that in the documentation – Paul Feb 07 '22 at 21:46

0 Answers0