How to change background color after editing QTableView cell?

I have this QTableView with a custom model and a delegate, how do I change the background color of a cell after editing it?

should I do this in the setModelData() ?

 index.model.setData(index, QVariant(True),Qt.UserRole) 

and then in the data() # model calling itself?

 if role == Qt.BackgroundColorRole: if index.model().data(index,Qt.UserRole).toBool(): return QVariant(QColor(Qt.darkBlue)) 

and in the setData() model, I have no code:

 if role==Qt.UserRole: .... 

What is the right way to do this?

edit: Here is my setData() method in the user model

 def setData(self, index, value, role=Qt.EditRole): if index.isValid() and 0 <= index.row() < len(self.particles): particle = self.particles[index.row()] column = index.column() if column == ID: value,ok= value.toInt() if ok: particle.id =value elif column == CYCLEIDANDNAME: cycleId,cycleName= value.toString().split(' ') particle.cycleId =cycleId # also need to set cycleName for name in self.cycleNames: if name.endsWith(cycleName): particle.cycleFrameNormalized=particle.cycleName = name break elif column == CYCLEFRAME: value,ok= value.toInt() if ok: print 'set new val to :',value particle.cycleFrame =value # self.setData(index,QVariant(QColor(Qt.red)),Qt.BackgroundRole) elif column == CLASSID: value,ok= value.toInt() if ok: particle.classId =value elif column == VARIATIONID: value,ok= value.toInt() if ok: particle.variationId =value self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index) return True return False 

Sorry, still has no idea, I will insert the full code from the gui quick development example: I posted my code here http://pastebin.com/ShgRRMcY

How can I change the code to change the background color of the cell after editing the cell?

+4
source share
2 answers

You need to somehow track the edited elements in the model. You do not need UserRole . You can save this internally, but of course, if you want to expose this information outside, UserRole perfect for this.

Here is a simple example. You can configure it to your code:

 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_()) 
+3
source

QTableWidget has an itemChanged signal, which must be connected to the slot . After connecting to the slot, the signal will pass to the QTableWidgetItem , which has been changed. From there, you can use methods for QTableWidgetItem , such as setBackgroundColor , to change the background.

Here is an example

 #! /usr/bin/env python2.7 from PyQt4.QtCore import * from PyQt4.QtGui import * import sys class Main(QTableWidget): def __init__(self): super(Main, self).__init__(2, 5) layout = QHBoxLayout(self) self.itemChanged.connect(self.changeBG) def changeBG(self, cell): cell.setBackgroundColor(QColor(225, 0, 225)) if __name__ == "__main__": app = QApplication(sys.argv) main = Main() main.show() app.exec_() 
-1
source

All Articles