基于Upd值的PyQt闪烁背景色

2024-04-20 09:37:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在根据更改的值设置QWidgetItem的背景色时遇到问题。在

我有下面的设置,它只是根据单击按钮将随机数生成到QTableWidget中。在

如果新值高于或低于旧值,我想使单元格的背景发生变化。例如,如果新值较高,则闪烁蓝色一秒钟(或半秒钟),或者如果新值较低,则闪烁/闪烁黄色一段时间。在

在这个过程中,我不知道从哪里开始。在

非常感谢

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from random import randint

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 350, 380)
        self.createTable()
        self.button = QPushButton('Update Values', self)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)
        self.button.clicked.connect(self.on_click)
        self.show()

    def createTable(self):
        self.tableWidget = QTableWidget()
        self.nrows=10
        self.ncols=3
        self.tableWidget.setRowCount(self.nrows)
        self.tableWidget.setColumnCount(self.ncols)

        for i in range(self.nrows):
            for j in range(self.ncols):
                self.tableWidget.setItem(i, j, QTableWidgetItem('{}'.format(randint(0,9))))

        self.tableWidget.move(0,0)
        self.tableWidget.doubleClicked.connect(self.on_click)

    @pyqtSlot()
    def on_click(self):
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.tableWidget.setItem(i, j, QTableWidgetItem('{}'.format(randint(0,9))))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Tags: infromimportselffordefsysrange
1条回答
网友
1楼 · 发布于 2024-04-20 09:37:54

可能有许多不同的方法来实现这一点。一种方法是创建QTableWidgetItem的子类并重写setData方法。这将允许您监视特定的item data role的哪些值正在更改。(如果您使用QTableWidgem.itemChanged()这样的信号,这是不可能的,因为它没有给您角色)。在

这样做的唯一要记住的是,它只能在项添加到表中之后监视更改。因此,您需要先添加空白项,然后再更新所有值。在

改变背景颜色的机制要简单得多,因为它只需要一个single-shot timer。在

以下是基于您的示例的上述所有演示:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QColor
from PyQt5.QtCore import pyqtSlot, Qt, QTimer
from random import randint

class TableWidgetItem(QTableWidgetItem):
    def setData(self, role, value):
        if role == Qt.DisplayRole:
            try:
                newvalue = int(value)
                oldvalue = int(self.data(role))
            except (ValueError, TypeError):
                pass
            else:
                if newvalue != oldvalue:
                    if newvalue > oldvalue:
                        color = QColor('aliceblue')
                    elif newvalue < oldvalue:
                        color = QColor('lightyellow')
                    def update_background(color=None):
                        super(TableWidgetItem, self).setData(
                            Qt.BackgroundRole, color)
                    update_background(color)
                    QTimer.singleShot(2000, update_background)
            super(TableWidgetItem, self).setData(role, value)

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(700, 100, 350, 380)
        self.createTable()
        self.button = QPushButton('Update Values', self)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)
        self.button.clicked.connect(self.populateTable)
        self.show()

    def createTable(self):
        self.tableWidget = QTableWidget()
        self.nrows=10
        self.ncols=3
        self.tableWidget.setRowCount(self.nrows)
        self.tableWidget.setColumnCount(self.ncols)

        for i in range(self.nrows):
            for j in range(self.ncols):
                self.tableWidget.setItem(i, j, TableWidgetItem())

        self.tableWidget.move(0,0)
        self.tableWidget.doubleClicked.connect(self.populateTable)
        self.populateTable()

    @pyqtSlot()
    def populateTable(self):
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.tableWidget.item(i, j).setText('{}'.format(randint(0,9)))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

相关问题 更多 >