不显示鼠标悬停颜色

This commit is contained in:
Irony 2019-11-08 14:31:16 +08:00
parent 66582ba6ac
commit 066d15c788

View file

@ -10,7 +10,8 @@ Created on 2019年11月8日
@description: 父节点不可选中 @description: 父节点不可选中
""" """
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QStyledItemDelegate,\
QStyle
__Author__ = 'Irony' __Author__ = 'Irony'
@ -18,10 +19,23 @@ __Copyright__ = 'Copyright (c) 2019'
__Version__ = 1.0 __Version__ = 1.0
class NoColorItemDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
if option.state & QStyle.State_HasFocus:
# 取消虚线框
option.state = option.state & ~ QStyle.State_HasFocus
if option.state & QStyle.State_MouseOver and index.data(Qt.UserRole + 1):
# 不显示鼠标悬停颜色
option.state = option.state & ~ QStyle.State_MouseOver
super(NoColorItemDelegate, self).paint(painter, option, index)
class Window(QTreeWidget): class Window(QTreeWidget):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs) super(Window, self).__init__(*args, **kwargs)
self.setItemDelegateForColumn(0, NoColorItemDelegate(self))
# 父节点(不可选中) # 父节点(不可选中)
pitem1 = QTreeWidgetItem(self, ['parent item 1']) pitem1 = QTreeWidgetItem(self, ['parent item 1'])