我有自定义模型和委托的QTableView,如何在编辑后更改单元格的背景颜色?
我是否应该在代理人setModelData()
?
index.model.setData(index, QVariant(True),Qt.UserRole)
以及之后的模型data()
#它正在调用自己?
if role == Qt.BackgroundColorRole:
if index.model().data(index,Qt.UserRole).toBool():
return QVariant(QColor(Qt.darkBlue))
并且在模型的setData()
中我没有像:
if role==Qt.UserRole:
....
这样做的正确方法是什么?
编辑:
这是我在自定义模型中的整个setData()
方法
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
如何在编辑单元格后更改代码以使单元格背景颜色发生变化?
答案 0 :(得分:4)
您需要以某种方式跟踪模型中已编辑的项目。您不需要UserRole
。您可以在内部保留此信息,但当然,如果您希望将此信息公开给外部UserRole
,则可以使用此信息。
这是一个简单的例子。您可以根据代码进行调整:
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_())
答案 1 :(得分:-1)
QTableWidget
的信号itemChanged
需要连接到slot
。连接到插槽后,信号将传入已更改的QTableWidgetItem
。从那里,您可以使用QTableWidgetItem
的方法,例如setBackgroundColor
来更改背景。
这是一个例子
#! /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_()