我可以使用QSqlTableModel对QComboBox中的行进行着色吗?
我试过了:
a=self.ui.comboBox.model().index(0,0)
self.ui.comboBox.model().setData(a, QtGui.QColor('red'), 8)
之后尝试使用processEvents或setModel进行各种更新,但没有一个更新。
答案 0 :(得分:1)
有几种方法可以为某些行更改背景颜色。
class CustomSqlModel(QtSql.QSqlTableModel):
def __init__(self, parent=None):
QtSql.QSqlTableModel.__init__(self, parent=parent)
def data(self, index, role):
if role == QtCore.Qt.BackgroundRole:
if index.row() in [2, 4, 5]:
return QtGui.QBrush(QtCore.Qt.yellow)
return QtSql.QSqlTableModel.data(self, index, role)
示例:
combo = QComboBox()
model = CustomSqlModel()
model.setTable("table")
combo.setModel(model)
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
QtWidgets.QStyledItemDelegate.__init__(self, parent=parent)
def paint(self, painter, option, index):
if index.row() in [1, 4, 5]:
painter.save()
painter.fillRect(option.rect, QtGui.QBrush(QtCore.Qt.red))
painter.restore()
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
示例:
combo = QComboBox()
model = QSqlTableModel()
model.setTable("table")
combo.setModel(model)
combo.setItemDelegate(StyledItemDelegate())