2017-10-03 15 views
1

変更された値に基づいてQWidgetItemの背景色を設定する際に問題があります。PyQt - 値の更新に基づく点滅背景色

ボタンのクリックに基づいて単純に乱数をQTableWidgetに生成する以下の設定があります。

新しい値が古い値よりも高いか低い場合、セルの背景が変化するようにしたいと思います。たとえば、新しい値が高い場合は、青色を1秒間(または0.5秒間)点滅させるか、新しい値がより低い場合にしばらくの間黄色で点滅/点滅させます。

私はこのプロセスでどこから始めるのか分かりません。

感謝

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

答えて

1

これを実装するさまざまな方法は、おそらくあります。 1つの方法は、QTableWidgetItemのサブクラスを作成し、setDataメソッドをオーバーライドすることです。これにより、特定の値に対して変更されている値を監視することができます。item data roleQTableWidgem.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_()) 
+0

は素敵な方法と明確な説明のためにありがとうございました。まさに私が必要としたもので、あなたが与えた参考文献も勉強します。乾杯! –

関連する問題