如何在PyQt4的QListWidget中制作同步滚动条
How to make synchronized Scroll bar in QListWidget in PyQt4
我有两个列表小部件,我想让两个列表的滚动条同步。我是 PyQt4 的新手,所以我不知道该怎么做。
我的代码如下
代码:
from PyQt4 import QtGui,QtCore
import sys
def window():
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
main_horizontal = QtGui.QHBoxLayout()
verti_1 = QtGui.QVBoxLayout()
verti_2 = QtGui.QVBoxLayout()
list1 = QtGui.QListWidget()
for i in range(20):
list1.addItem(str(i))
list2 = QtGui.QListWidget()
for i in range(20):
list2.addItem("name" + str(i))
verti_1.addWidget(list1)
verti_2.addWidget(list2)
main_horizontal.addLayout(verti_1)
main_horizontal.addLayout(verti_2)
win.setLayout(main_horizontal)
win.resize(400,200)
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
window()
输出:
预期输出
我想让这两个滚动条同步
解决方案是将 verticalScrollBar()
的 valueChanged
信号连接到另一个 verticalScrollBar()
移动的插槽,但这可能会产生无限循环,因此您必须避免它使用 blockSignals()
如下所示:
import sys
from functools import partial
from PyQt4 import QtGui,QtCore
def move_scrollbar(vs, value):
vs.blockSignals(True)
vs.setValue(value)
vs.blockSignals(False)
def window():
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
main_horizontal = QtGui.QHBoxLayout()
verti_1 = QtGui.QVBoxLayout()
verti_2 = QtGui.QVBoxLayout()
list1 = QtGui.QListWidget()
for i in range(20):
list1.addItem(str(i))
list2 = QtGui.QListWidget()
for i in range(20):
list2.addItem("name" + str(i))
verti_1.addWidget(list1)
verti_2.addWidget(list2)
vs1 = list1.verticalScrollBar()
vs2 = list2.verticalScrollBar()
vs1.valueChanged.connect(partial(move_scrollbar, vs2))
vs2.valueChanged.connect(partial(move_scrollbar, vs1))
main_horizontal.addLayout(verti_1)
main_horizontal.addLayout(verti_2)
win.setLayout(main_horizontal)
win.resize(400,200)
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
window()
我有两个列表小部件,我想让两个列表的滚动条同步。我是 PyQt4 的新手,所以我不知道该怎么做。
我的代码如下
代码:
from PyQt4 import QtGui,QtCore
import sys
def window():
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
main_horizontal = QtGui.QHBoxLayout()
verti_1 = QtGui.QVBoxLayout()
verti_2 = QtGui.QVBoxLayout()
list1 = QtGui.QListWidget()
for i in range(20):
list1.addItem(str(i))
list2 = QtGui.QListWidget()
for i in range(20):
list2.addItem("name" + str(i))
verti_1.addWidget(list1)
verti_2.addWidget(list2)
main_horizontal.addLayout(verti_1)
main_horizontal.addLayout(verti_2)
win.setLayout(main_horizontal)
win.resize(400,200)
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
window()
输出:
预期输出
我想让这两个滚动条同步
解决方案是将 verticalScrollBar()
的 valueChanged
信号连接到另一个 verticalScrollBar()
移动的插槽,但这可能会产生无限循环,因此您必须避免它使用 blockSignals()
如下所示:
import sys
from functools import partial
from PyQt4 import QtGui,QtCore
def move_scrollbar(vs, value):
vs.blockSignals(True)
vs.setValue(value)
vs.blockSignals(False)
def window():
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
main_horizontal = QtGui.QHBoxLayout()
verti_1 = QtGui.QVBoxLayout()
verti_2 = QtGui.QVBoxLayout()
list1 = QtGui.QListWidget()
for i in range(20):
list1.addItem(str(i))
list2 = QtGui.QListWidget()
for i in range(20):
list2.addItem("name" + str(i))
verti_1.addWidget(list1)
verti_2.addWidget(list2)
vs1 = list1.verticalScrollBar()
vs2 = list2.verticalScrollBar()
vs1.valueChanged.connect(partial(move_scrollbar, vs2))
vs2.valueChanged.connect(partial(move_scrollbar, vs1))
main_horizontal.addLayout(verti_1)
main_horizontal.addLayout(verti_2)
win.setLayout(main_horizontal)
win.resize(400,200)
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
window()