im trying to change the background color of a qtable cell after editing it. i already watched the qustion "How to change background color after editing QTableView cell?", the problem i have is, in the example from the answer, i dont understand where "role" comes from. i mean, where is role declared and where role change its value? i only see where role is compared to "Qt.Core.xxx"
import sys
from PyQt4 import QtGui, QtCore
class Model(QtCore.QAbstractTableModel):
def __init__(self, parent=None):
super(Model, self).__init__(parent)
# list of lists containing [data for cell, changed]
self._data = [[['%d - %d' % (i, j), False] for j in range(10)] for i in range(10)]
def rowCount(self, parent):
return len(self._data)
def columnCount(self, parent):
return len(self._data[0])
def flags(self, index):
return QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled |QtCore.Qt.ItemIsEditable
def data(self, index, role):
if index.isValid():
data, changed = self._data[index.row()][index.column()]
if role in [QtCore.Qt.DisplayRole, QtCore.Qt.EditRole]:
return data
if role == QtCore.Qt.BackgroundRole and changed:
return QtGui.QBrush(QtCore.Qt.darkBlue)
def setData(self, index, value, role):
if role == QtCore.Qt.EditRole:
# set the new value with True `changed` status
self._data[index.row()][index.column()] = [value.toString(), True]
self.dataChanged.emit(index, index)
return True
return False
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
t = QtGui.QTableView()
m = Model(t)
t.setModel(m)
t.show()
sys.exit(app.exec_())
i cant just add a comment in the other question, because i dont have 50 "repution-points", sry.