Hyun-geun Kim
Hyun-geun Kim

Reputation: 1069

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?

Upvotes: 2

Views: 4208

Answers (3)

warvariuc
warvariuc

Reputation: 59594

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).

Upvotes: 1

ekhumoro
ekhumoro

Reputation: 120588

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

Upvotes: 7

user1092803
user1092803

Reputation: 3277

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

Upvotes: 0

Related Questions