i have the following doubt regarding QTableView, i have added some code that changes the row background depending on what string the dataframe contains on the last column.
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.BackgroundRole:
if df.iloc[index.row(),5] == "Ready for QC":
return QBrush(Qt.yellow)
if df.iloc[index.row(),5] == "In Progress":
return QBrush(Qt.green)
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
When the table loads the first time it properly paints the Table, the thing is that i have a specific part of the code that always runs every 5 seconds as to refresh the Table with fresh information.
def printit():
threading.Timer(5.0, printit).start()
weekNumber = date.today().isocalendar()[1]
aux = pd.read_excel('PCS tasks 2020.xlsm',sheet_name='W'+str(weekNumber))
today = datetime.today()
df = aux[aux['Date Received'] == today.strftime("%Y-%d-%m")]
df = df[["Requestor","Subject","Task type","Created by","QC Executive","Status"]].fillna("")
df = df[df['Status'] != "Completed"]
model = pandasModel(df)
view.setModel(None)
view.setModel(model)
The thing is that when the above code runs, the table does in fact update the data, but it does not change the colors. I currently have tried different methods like defining setData and in there update the colors but with no avail. Now im asking you if someone knows something regarding updating colors on a QTableView.
By the way, I'm attaching the entire code for the python program below as to give context.
import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5.QtGui import QBrush
from datetime import date, datetime
import threading
weekNumber = date.today().isocalendar()[1]
aux = pd.read_excel('PCS tasks 2020.xlsm',sheet_name='W'+str(weekNumber))
today = datetime.today()
df = aux[aux['Date Received'] == today.strftime("%Y-%d-%m")]
df = df[["Requestor","Subject","Task type","Created by","QC Executive","Status"]].fillna("")
df = df[df['Status'] != "Completed"]
def printit():
threading.Timer(5.0, printit).start()
weekNumber = date.today().isocalendar()[1]
aux = pd.read_excel('PCS tasks 2020.xlsm',sheet_name='W'+str(weekNumber))
today = datetime.today()
df = aux[aux['Date Received'] == today.strftime("%Y-%d-%m")]
df = df[["Requestor","Subject","Task type","Created by","QC Executive","Status"]].fillna("")
df = df[df['Status'] != "Completed"]
model = pandasModel(df)
view.setModel(None)
view.setModel(model)
class pandasModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1] -1
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.BackgroundRole:
if df.iloc[index.row(),5] == "Ready for QC":
return QBrush(Qt.yellow)
if df.iloc[index.row(),5] == "In Progress":
return QBrush(Qt.green)
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
model = pandasModel(df)
view = QTableView()
view.setModel(model)
view.resize(523, 300)
printit()
view.show()
sys.exit(app.exec_())