Question

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?

Was it helpful?

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_())

OTHER TIPS

QListWidget inherits QListView, which inherits QAbstractItemView, which...

Anyway you can see all QListWidget members clicking on List of all members, including inherited members, where you find verticalScrollBar () const : QScrollBar * method, which is inherited from QAbstractScrollArea.

You can connect to the listWidget's vertical scrollbar valueChanged signal:

listWidget.verticalScrollBar().valueChanged.connect(onScrollBarValueChanged)

Where onScrollBarValueChanged is the slot (signal handler).

To track the scrollbar movement, you must reimplement the scrollContentsBy() virtual function. Take a look also to the QAbstractSlider::actionTriggered() signal and to the sliderPosition property

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top