From 066d15c788a6a61ead73e890a25581b49a9b003a Mon Sep 17 00:00:00 2001 From: Irony <892768447@qq.com> Date: Fri, 8 Nov 2019 14:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA=E9=BC=A0=E6=A0=87?= =?UTF-8?q?=E6=82=AC=E5=81=9C=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QTreeWidget/ParentNodeForbid.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/QTreeWidget/ParentNodeForbid.py b/QTreeWidget/ParentNodeForbid.py index c422809..18759dd 100644 --- a/QTreeWidget/ParentNodeForbid.py +++ b/QTreeWidget/ParentNodeForbid.py @@ -10,7 +10,8 @@ Created on 2019年11月8日 @description: 父节点不可选中 """ from PyQt5.QtCore import Qt -from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem +from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QStyledItemDelegate,\ + QStyle __Author__ = 'Irony' @@ -18,10 +19,23 @@ __Copyright__ = 'Copyright (c) 2019' __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): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) + self.setItemDelegateForColumn(0, NoColorItemDelegate(self)) # 父节点(不可选中) pitem1 = QTreeWidgetItem(self, ['parent item 1'])