PyQt4 QTableView 单元格文本在行选择时更改颜色
PyQt4 QTableView cell text changes color on row selection
我正在开发一个使用 PyQt4 比较 *.xls 和 *.xlsx 文件的小应用程序。
这是比较 window 的样子。该行以浅红色突出显示,而单元格文本为亮红色。
当我 select 单击该行时,所有红色突出显示都消失了。
在这种情况下,有什么方法可以使标有红色的单元格保持原样吗?
我为每个 QtGui.QTableView
使用 QtCore.QAbstractTableModel
def data(self, index, role):
"""
Updating tableView data
"""
#########################################################
# Data role -> Updating cell contents #
#########################################################
if role == QtCore.Qt.DisplayRole:
row = index.row()
if 0 <= row < self.rowCount():
column = index.column()
if 0 <= column < self.columnCount():
return self._data[row][column]
#########################################################
# Background role -> Updating row color #
#########################################################
if role == QtCore.Qt.BackgroundRole and index.row() in self._marked_rows:
customColor = QtGui.QColor(255, 204, 204)
return QtCore.QVariant(QtGui.QBrush(customColor))
#########################################################
# TextColorRole role -> Updating cell text color #
#########################################################
if role == QtCore.Qt.ForegroundRole:
if str(index.row()) + ':' + str(index.column()) in self._marked_cells:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.red))
经过一些搜索,我尝试使用 QtGui.QStyledItemDelegate。
现在文本保持其颜色,但没有突出显示,我不知道如何保持该行的浅红色。
class TextColorDelegate(QtGui.QStyledItemDelegate):
def __init__(self, cells, parent = None):
QtGui.QStyledItemDelegate.__init__(self, parent)
self.cells = cells
def paint(self, painter, option, index):
painter.save()
value = index.data(QtCore.Qt.DisplayRole)
if value.isValid():
text = value.toString()
if str(index.row()) + ':' + str(index.column()) in self.cells:
painter.setPen(QtGui.QPen(QtCore.Qt.red))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)
else:
painter.setPen(QtGui.QPen(QtCore.Qt.black))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)
painter.restore()
self.model_1 = TableModel(_data_1,
columns_string,
_markedRows_1,
_markedCells_1,
self.appPath,
self.tableView_1)
delegate_1 = TextColorDelegate(_markedCells_1, self)
self.tableView_1.setModel(self.model_1)
self.tableView_1.setItemDelegate(delegate_1)
有什么方法可以定位那些特定的单元格并使该行突出显示吗?
我设法自己找到了解决方案。也许可以使用更简单的方法,但它确实有效。
class TextColorDelegate(QtGui.QStyledItemDelegate):
"""
Delegate used for changing text color and highlighting behaviour
"""
def __init__(self, cells, parent = None):
"""
Object initialization
cells - marked cells that have different content
"""
QtGui.QStyledItemDelegate.__init__(self, parent)
self.cells = cells
def paint(self, painter, option, index):
"""
Painter function used for overriding display behaviour
"""
painter.save()
displayText = index.data(QtCore.Qt.DisplayRole)
backgroudColor = index.data(QtCore.Qt.BackgroundColorRole)
customColor = QtGui.QColor(255, 204, 204)
blackColor = QtCore.Qt.black
yellowColor = QtCore.Qt.yellow
redColor = QtCore.Qt.red
backgroundFlag = backgroudColor.isValid()
textFlag = displayText.isValid()
textContent = displayText.toString()
if backgroudColor.isValid():
###################################################################
# Evaluating rows with differences #
# Adjusting background and text color depending on QStyleState #
###################################################################
painter.fillRect(option.rect, customColor) #set row background color
if (option.state & QtGui.QStyle.State_Selected):
painter.fillRect(option.rect, option.palette.highlight())
color_to_set = yellowColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor
else:
color_to_set = redColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor
###################################################################
# Evaluating rows with no differences #
# Adjusting background and text color depending on QStyleState #
###################################################################
else:
if (option.state & QtGui.QStyle.State_Selected) : painter.fillRect(option.rect, option.palette.highlight())
color_to_set = blackColor
if textFlag:
painter.setPen(QtGui.QPen(color_to_set))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, textContent)
painter.restore()
现在突出显示有效。
https://i.imgur.com/drpliS4.jpg
https://i.imgur.com/RiO0d4I.jpg
我正在开发一个使用 PyQt4 比较 *.xls 和 *.xlsx 文件的小应用程序。
这是比较 window 的样子。该行以浅红色突出显示,而单元格文本为亮红色。
当我 select 单击该行时,所有红色突出显示都消失了。
在这种情况下,有什么方法可以使标有红色的单元格保持原样吗?
我为每个 QtGui.QTableView
使用 QtCore.QAbstractTableModeldef data(self, index, role):
"""
Updating tableView data
"""
#########################################################
# Data role -> Updating cell contents #
#########################################################
if role == QtCore.Qt.DisplayRole:
row = index.row()
if 0 <= row < self.rowCount():
column = index.column()
if 0 <= column < self.columnCount():
return self._data[row][column]
#########################################################
# Background role -> Updating row color #
#########################################################
if role == QtCore.Qt.BackgroundRole and index.row() in self._marked_rows:
customColor = QtGui.QColor(255, 204, 204)
return QtCore.QVariant(QtGui.QBrush(customColor))
#########################################################
# TextColorRole role -> Updating cell text color #
#########################################################
if role == QtCore.Qt.ForegroundRole:
if str(index.row()) + ':' + str(index.column()) in self._marked_cells:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.red))
经过一些搜索,我尝试使用 QtGui.QStyledItemDelegate。 现在文本保持其颜色,但没有突出显示,我不知道如何保持该行的浅红色。
class TextColorDelegate(QtGui.QStyledItemDelegate):
def __init__(self, cells, parent = None):
QtGui.QStyledItemDelegate.__init__(self, parent)
self.cells = cells
def paint(self, painter, option, index):
painter.save()
value = index.data(QtCore.Qt.DisplayRole)
if value.isValid():
text = value.toString()
if str(index.row()) + ':' + str(index.column()) in self.cells:
painter.setPen(QtGui.QPen(QtCore.Qt.red))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)
else:
painter.setPen(QtGui.QPen(QtCore.Qt.black))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, text)
painter.restore()
self.model_1 = TableModel(_data_1,
columns_string,
_markedRows_1,
_markedCells_1,
self.appPath,
self.tableView_1)
delegate_1 = TextColorDelegate(_markedCells_1, self)
self.tableView_1.setModel(self.model_1)
self.tableView_1.setItemDelegate(delegate_1)
有什么方法可以定位那些特定的单元格并使该行突出显示吗?
我设法自己找到了解决方案。也许可以使用更简单的方法,但它确实有效。
class TextColorDelegate(QtGui.QStyledItemDelegate):
"""
Delegate used for changing text color and highlighting behaviour
"""
def __init__(self, cells, parent = None):
"""
Object initialization
cells - marked cells that have different content
"""
QtGui.QStyledItemDelegate.__init__(self, parent)
self.cells = cells
def paint(self, painter, option, index):
"""
Painter function used for overriding display behaviour
"""
painter.save()
displayText = index.data(QtCore.Qt.DisplayRole)
backgroudColor = index.data(QtCore.Qt.BackgroundColorRole)
customColor = QtGui.QColor(255, 204, 204)
blackColor = QtCore.Qt.black
yellowColor = QtCore.Qt.yellow
redColor = QtCore.Qt.red
backgroundFlag = backgroudColor.isValid()
textFlag = displayText.isValid()
textContent = displayText.toString()
if backgroudColor.isValid():
###################################################################
# Evaluating rows with differences #
# Adjusting background and text color depending on QStyleState #
###################################################################
painter.fillRect(option.rect, customColor) #set row background color
if (option.state & QtGui.QStyle.State_Selected):
painter.fillRect(option.rect, option.palette.highlight())
color_to_set = yellowColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor
else:
color_to_set = redColor if ( str( index.row() ) + ':' + str( index.column() ) ) in self.cells else blackColor
###################################################################
# Evaluating rows with no differences #
# Adjusting background and text color depending on QStyleState #
###################################################################
else:
if (option.state & QtGui.QStyle.State_Selected) : painter.fillRect(option.rect, option.palette.highlight())
color_to_set = blackColor
if textFlag:
painter.setPen(QtGui.QPen(color_to_set))
painter.drawText(option.rect, QtCore.Qt.AlignVCenter, textContent)
painter.restore()
现在突出显示有效。
https://i.imgur.com/drpliS4.jpg
https://i.imgur.com/RiO0d4I.jpg