PyQt4 : is there any signal related to scrollbar?

非 Y 不嫁゛ 提交于 2019-12-08 08:31:29

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!