I have a subclassed QAbstractTableModel
which contains None Type Values to display the rows as empty
class ViewModel(qtc.QAbstractTableModel):
def __init__(self, input_data=None):
super().__init__()
self.input_data = input_data or [[None, None],[None, None]]
I want to change the colors of the cells if the inserted values (replacing the None Type Values) are in /out a certain range
like z = 12 <= x <= 20
# False == red, True == green
the color should only change if the items are edited or
self.model.layoutChanged.emit()
gets executed
the answers to change cell's background color of a QTableView are suggesting to make changes in the data method How to change cell's background color of a QTableView [duplicate] but this is going to color the cells right at the beginning
I have found an approach in the QT forum to insert a proxy model between the view and the model How to simply change the background color of a cell inside a TableView
I'm trying to implement the code into Pyqt5, unsuccessful so far. Is there a PyQt5 approach which I'm unaware of?
example code
import sys
import re
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
class ViewModel(qtc.QAbstractTableModel):
def __init__(self, input_data=None):
super().__init__()
self.input_data = input_data or [[None, None],[None, None]]
def data(self, index, role): # parameter index, role are needed !
if role == qtc.Qt.DisplayRole:
try:
text = self.input_data[index.row()][index.column()]
except IndexError:
text = None
return text
def rowCount(self, index=qtc.QModelIndex()):
return 0 if index.isValid() else len(self.input_data)
def columnCount(self, index):
return len(self.input_data[0])
def headerData(self, section, orientation, role):
# section is which index of the orientation
if role == qtc.Qt.DisplayRole:
if orientation == qtc.Qt.Vertical:
return "row"
def flags(self, index):
return qtc.Qt.ItemIsEditable | qtc.Qt.ItemIsSelectable | qtc.Qt.ItemIsEnabled
def setData(self, index, value, role=qtc.Qt.EditRole):
if role == qtc.Qt.EditRole:
try:
row = index.row()
column = index.column()
pattern = '^[\d]+(?:,[\d]+)?$'
if re.fullmatch(pattern, value, flags=0):
print("true")
self.input_data[row][column] = value # float
else:
print("nope")
pass
return True
except ValueError:
print("not a number")
return False
class MainWindow(qtw.QWidget):
def __init__(self):
super().__init__()
# View
table_view = qtw.QTableView()
self.model = ViewModel()
table_view.setModel(self.model)
# size and position
qtRectangle = self.frameGeometry()
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
# size
self.resize(1000, 410)
# layout
qvboxlayout = qtw.QVBoxLayout()
qvboxlayout.addWidget(table_view)
self.setLayout(qvboxlayout)
self.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
If you want to use a proxy to change the color then only the data()
method should be overridden:
class ColorProxy(qtc.QIdentityProxyModel):
def data(self, index, role=qtc.Qt.DisplayRole):
if role == qtc.Qt.BackgroundRole:
data = index.data()
try:
value = float(data)
except (ValueError, TypeError) as e:
print("error:", e)
else:
return qtg.QColor("green") if 12 <= value <= 20 else qtg.QColor("red")
return super().data(index, role)
# ...
self.model = ViewModel()
proxy = ColorProxy()
proxy.setSourceModel(self.model)
table_view.setModel(proxy)
# ...
Another solution is to use a delegate
class ColorDelegate(qtw.QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
data = index.data()
try:
value = float(data)
except (ValueError, TypeError) as e:
print("error:", e)
else:
color = qtg.QColor("green") if 12 <= value <= 20 else qtg.QColor("red")
option.backgroundBrush = color
# ...
table_view = qtw.QTableView()
self.model = ViewModel()
delegate = ColorDelegate(table_view)
table_view.setItemDelegate(delegate)
table_view.setModel(self.model)
# ...