PyQt4:是否有与滚动条相关的信号?

问题描述:

我计划创建两个 listWidget 并且它们具有相同数量的列表.因此,当一个 listWidget 上下滚动时,另一个也会移动它的列表.但是,我找不到相关信号.我错过了什么吗?

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?

您需要将一个滚动条的 valueChanged 信号连接到另一个滚动条的 setValue 槽(反之亦然).

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

乍一看,这似乎是危险的递归,但 Qt 似乎可以毫无问题地处理它,如下例所示:

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