pyqtscrollbarpyqt4signals-slotsqlistwidget

PyQt4 : is there any signal related to scrollbar?


I plan to create a two listWidget and they have same amount of list. So, when a listWidget scroll up and down, another one also travel it's list. But, I can't find related signal. Did I miss something?


Solution

  • You need to connect the valueChanged signals of one scrollbar to the setValue slot of the other scrollbar (and vice versa).

    At first glance, this might seem dangerously recursive, but Qt seems to handle it without any problem, as this example shows:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QWidget):
        def __init__(self):
            QtGui.QWidget.__init__(self)
            self.listA = QtGui.QListWidget(self)
            self.listB = QtGui.QListWidget(self)
            layout = QtGui.QHBoxLayout(self)
            layout.addWidget(self.listA)
            layout.addWidget(self.listB)
            for index in range(100):
                self.listA.addItem('Sample text for Item %d' % index)
                self.listB.addItem('Sample text for Item %d' % index)
            self.listA.horizontalScrollBar().valueChanged.connect(
                self.listB.horizontalScrollBar().setValue)
            self.listB.horizontalScrollBar().valueChanged.connect(
                self.listA.horizontalScrollBar().setValue)
            self.listA.verticalScrollBar().valueChanged.connect(
                self.listB.verticalScrollBar().setValue)
            self.listB.verticalScrollBar().valueChanged.connect(
                self.listA.verticalScrollBar().setValue)
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())