[QT] Scroll multiple lists with one scrollbar

Laura Creighton lac at openend.se
Thu May 28 08:18:03 EDT 2015


Looks like what you need to do is to 
connect verticalScrollBar().valueChanged (in one widget) to
verticalScrollBar().setValue in all of the others.

So is this code on the right track?  It uses lists, not labels, and
doesn't hide the scrollbars, but is this the behaviour you need (
and weren't getting)?  It should work for labels too..


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)
        self.listC = QtGui.QListWidget(self)

        widgets=[self.listA, self.listB, self.listC]
        layout = QtGui.QHBoxLayout(self)

        for w1 in widgets:
            layout.addWidget(w1)
            for index in range(100):
                w1.addItem('This is line number %d' % (index +1))
            for w2 in widgets:
                if w1 != w2:
                    w1.verticalScrollBar().valueChanged.connect(w2.verticalScrollBar().setValue)
            
if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())



More information about the Python-list mailing list