禁止父节点
This commit is contained in:
parent
db563fe6a3
commit
66582ba6ac
4 changed files with 89 additions and 1 deletions
78
QTreeWidget/ParentNodeForbid.py
Normal file
78
QTreeWidget/ParentNodeForbid.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Created on 2019年11月8日
|
||||
@author: Irony
|
||||
@site: https://pyqt5.com https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: QTreeWidget.ParentNodeForbid
|
||||
@description: 父节点不可选中
|
||||
"""
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem
|
||||
|
||||
|
||||
__Author__ = 'Irony'
|
||||
__Copyright__ = 'Copyright (c) 2019'
|
||||
__Version__ = 1.0
|
||||
|
||||
|
||||
class Window(QTreeWidget):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Window, self).__init__(*args, **kwargs)
|
||||
|
||||
# 父节点(不可选中)
|
||||
pitem1 = QTreeWidgetItem(self, ['parent item 1'])
|
||||
# 设置不可选中
|
||||
pitem1.setFlags(pitem1.flags() & ~Qt.ItemIsSelectable)
|
||||
# 设置一个标识用于屏蔽鼠标事件
|
||||
pitem1.setData(0, Qt.UserRole + 1, True)
|
||||
|
||||
pitem2 = QTreeWidgetItem(self, ['parent item 2'])
|
||||
pitem2.setFlags(pitem2.flags() & ~Qt.ItemIsSelectable)
|
||||
pitem2.setData(0, Qt.UserRole + 1, True)
|
||||
|
||||
# 子节点(可选)
|
||||
citem1 = QTreeWidgetItem(pitem1, ['child item 1'])
|
||||
citem2 = QTreeWidgetItem(pitem2, ['child item 2'])
|
||||
|
||||
self.expandAll()
|
||||
|
||||
# 信号槽
|
||||
self.itemActivated.connect(self.onItemActivated)
|
||||
self.itemClicked.connect(self.onItemClicked)
|
||||
self.itemDoubleClicked.connect(self.onItemDoubleClicked)
|
||||
self.itemPressed.connect(self.onItemPressed)
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
# 鼠标点击事件,判断当前点击位置是否有item且满足标志则拦截鼠标事件
|
||||
item = self.itemAt(event.pos())
|
||||
if item and item.data(0, Qt.UserRole + 1):
|
||||
event.accept()
|
||||
return
|
||||
super(Window, self).mousePressEvent(event)
|
||||
|
||||
def onItemActivated(self, item, column):
|
||||
print('Activated', item.text(0), item, column)
|
||||
|
||||
def onItemClicked(self, item, column):
|
||||
print('Clicked', item.text(0), item, column)
|
||||
|
||||
def onItemDoubleClicked(self, item, column):
|
||||
print('DoubleClicked', item.text(0), item, column)
|
||||
|
||||
def onItemPressed(self, item, column):
|
||||
print('Pressed', item.text(0), item, column)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
import cgitb
|
||||
cgitb.enable(1, None, 5, '')
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
app = QApplication(sys.argv)
|
||||
w = Window()
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
|
@ -3,6 +3,7 @@
|
|||
- 目录
|
||||
- [通过json数据生成树形结构](#1通过json数据生成树形结构)
|
||||
- [点击父节点全选/取消全选子节点](#2点击父节点全选取消全选子节点)
|
||||
- [禁止父节点/禁止父节点](#3禁止父节点)
|
||||
|
||||
## 1、通过json数据生成树形结构
|
||||
[运行 ParsingJson.py](ParsingJson.py)
|
||||
|
@ -17,4 +18,12 @@
|
|||
|
||||
点击父节点全选/取消全选子节点
|
||||
|
||||
![testTreeWidget](ScreenShot/allSelectNode.png)
|
||||
![testTreeWidget](ScreenShot/allSelectNode.png)
|
||||
|
||||
## 3、禁止父节点
|
||||
[运行 ParentNodeForbid.py](ParentNodeForbid.py)
|
||||
|
||||
1. 父节点通过设置`pitem1.setFlags(pitem1.flags() & ~Qt.ItemIsSelectable)`为不可选
|
||||
2. 完全禁用点击等需要重写`mousePressEvent`事件并结合item的标志来判断
|
||||
|
||||
![ParentNodeForbid](ScreenShot/ParentNodeForbid.gif)
|
BIN
QTreeWidget/ScreenShot/ParentNodeForbid.gif
Normal file
BIN
QTreeWidget/ScreenShot/ParentNodeForbid.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
|
@ -67,6 +67,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
|
|||
- [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)
|
||||
- [拖拽显示为图片](Test/partner_625781186/12.1拖拽显示为图片)
|
||||
- [点击父节点全选/取消全选子节点](QTreeWidget/testTreeWidget.py)
|
||||
- [禁止父节点](QTreeWidget/ParentNodeForbid.py)
|
||||
- [QTableWidget](QTableWidget)
|
||||
- [Sqlalchemy动态拼接字段查询显示表格](QTableWidget/SqlQuery.py)
|
||||
- [TableWidget嵌入部件](QTableWidget/TableWidget.py)
|
||||
|
|
Loading…
Reference in a new issue