PyQt4:是否有与滚动条相关的信号? [英] PyQt4 : is there any signal related to scrollbar?

查看:40
本文介绍了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_())

这篇关于PyQt4:是否有与滚动条相关的信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆