我有一个QStandardItemModel显示在两个QTreeView中。第一个树状视图使用标准的QStyledItemDelegate,没有任何改动,并显示带有复选框、图标及其显示文本的项目。第二个视图应显示相同的树,尽管QStyledItemDelegate的自定义实现应执行以下操作:
项目是否为斜体/不可单击以及是否显示为红色都保存在自定义DataRole中,并使用index.data(自定义_角色)进行访问
我的问题是:我无法让文本显示为红色,我不知道如何删除复选框,或者如何使项目可选或不可选
以下是我所拥有的:
class PostProcessedDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self):
super().__init__()
def paint(self, painter, option, index):
custom_option = QtWidgets.QStyleOptionViewItem(option)
custom_painter = painter
if not index.data(Item.IS_PROCESSED):
custom_option.font.setItalic(True)
#ALSO MAKE ITEM NON-SELECTABLE HERE
if index.data(Item.HAS_PROCESSING_ERROR):
custom_painter.setPen(QtGui.QColor(255,0,0)) #THIS DOESNT HAVE ANY EFFECT
#REMOVE CHECKBOX BEFORE PAINTING
super().paint(custom_painter, custom_option, index)
无需重写paint方法,但要修改initStyleOption中的QStyleOptionViewItem,对于选择,必须重写QTreeView的selectionCommand方法:
相关问题 更多 >
编程相关推荐