![](/img/trans.png)
[英]How to change the color of scroll bar in table widget in PyQt5 Python
[英]PyQt5 - How to change specific item background color in List Widget
这个程序有一个简单的列表小部件。 如何在不单击列表中更改任何特定项目的背景颜色?
from PyQt5 import QtWidgets, uic, QtGui
import threading
app = QtWidgets.QApplication([])
ui = uic.loadUi("app.ui")
update_ui = True
ui.lst_positions.addItem("Item 1")
ui.lst_positions.addItem("Item 2")
ui.lst_positions.addItem("Item 3")
def ui_update():
while update_ui == True:
# Updating labels works fine with threading
ui.lbl_in_position.setText("Random text")
# This does not work with threading
item = ui.lst_positions.item(1)
item.setBackground(QtGui.QColor("red"))
ui_thread = threading.Thread(target=ui_update)
ui_thread.daemon = True
ui_thread.start()
ui.show()
app.exec()
任务很简单:
item = ui.lst_positions.item(item_number_to_change)
item.setBackground(QtGui.QColor("red"))
更新:
问题是您不应该从另一个线程更新 GUI,因为 GUI 的元素不是线程安全的,因此您会收到您在评论中指出的警告。 一种解决方案是创建一个具有信号(线程安全)的 QObject,以在线程之间交换信息。
# ...
class Signaller(QtCore.QObject):
textChanged = QtCore.pyqtSignal(str)
itemChanged = QtCore.pyqtSignal(int, QtGui.QColor)
def ui_update(qobject):
while update_ui == True:
qobject.textChanged.emit("Random text")
qobject.itemChanged.emit(i, QtGui.QColor("red"))
def foo(i, color):
item = ui.lst_positions.item(i)
if item:
item.setBackground(color)
signaller = Signaller()
signaller.textChanged.connect(ui.lbl_in_position.setText)
signaller.itemChanged.connect(foo)
ui_thread = threading.Thread(target=ui_update, args=(signaller,))
ui_thread.daemon = True
ui_thread.start()
# ...
注意:仅仅因为文本显示正确并不意味着它是正确的。 如果直接从另一个线程更新 GUI,Qt 不保证(这意味着它可能会失败)正确的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.