Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color after editing QTableView cell?

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

shall i do this in delegate's setModelData() ?

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

and later in model's data() # it's calling itself ?

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

and in model's setData() i don't have any code like:

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

what is the correct way of doing this ?

edit: Here is my whole setData() method in custom 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 clue, i'll paste full code from rapid gui developement book example: i posted my code here http://pastebin.com/ShgRRMcY

how do i change to code to make cell background color change after edting the cell ?

like image 691
Shuman Avatar asked Oct 22 '22 22:10

Shuman


1 Answers

You need to keep track of the edited items in the model somehow. You don't need UserRole. You can keep this internally, but of course, if you want to expose this information to the outside UserRole is perfect for this.

Here is a simple example. You can adjust this 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_())
like image 199
Avaris Avatar answered Nov 01 '22 07:11

Avaris