编辑QTableView单元格后如何更改背景颜色? [英] How to change background color after editing QTableView cell?

查看:100
本文介绍了编辑QTableView单元格后如何更改背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有自定义模型和委托的 QTableView,编辑后如何更改单元格的背景颜色?

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

我应该在委托的 setModelData() 中这样做吗?

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

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

然后在模型的 data() # 它调用自己?

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))

在模型的 setData() 中,我没有任何代码,例如:

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

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

这样做的正确方法是什么?

what is the correct way of doing this ?

这是我在自定义模型中的整个 setData() 方法

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

对不起,我仍然不知道,我将粘贴快速 gui 开发书示例中的完整代码:我在这里发布了我的代码http://pastebin.com/ShgRRMcY

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 ?

推荐答案

您需要以某种方式跟踪模型中已编辑的项目.您不需要 UserRole.您可以将其保留在内部,但当然,如果您想将此信息公开给外部,UserRole 非常适合此操作.

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_())

这篇关于编辑QTableView单元格后如何更改背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆