在treewidget中添加鼠标右击响应,比如添加树节点,删除树节点,编辑数节点的名字
在treewidget中添加鼠标右击响应,比如添加树节点,删除树节点,编辑数节点的名字
关键一步:
from GUI.Ui_atomTree import *
from PyQt5.QtWidgets import *
from lib.loadtemplate import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from main import GlobalVariable
import os
from docx import Document
class atomWindow(Ui_DockWidget):
def __init__(self, Where_DockWidget):
super(atomWindow, self).__init__() # 调用父类的构造函数
self.setupUi(Where_DockWidget)
self.refreshTreeWidget()
self.pushButton_4.clicked.connect(self.refreshTreeWidget)
#self.treeWidget.itemDoubleClicked['QTreeWidgetItem*', 'int'].connect(self.slotDoubleClicked)
#self.treeWidget.itemDoubleClicked['QTreeWidgetItem*', 'int'].connect(self.treeWidget.expandAll)
#self.treeWidget.itemDoubleClicked['QTreeWidgetItem*', 'int'].connect(self.treeWidgetClicked)
#self.customContextMenuRequested['QPoint'].connect(self.rightMenuShow)
self.treeWidget.customContextMenuRequested['QPoint'].connect(self.showRightMenu)
def slotDoubleClicked(self, item, column):
rootPath = GlobalVariable.GlobalVar.getInstance().getValue("atomRootPath")
AtomName = item.text(0)
# print("双击6666 "+ AtomName + " " + str(column))
wordPath = rootPath + '/' + AtomName + '/' + AtomName + '.docx'
print(wordPath)
def mkAtomDirAndWord(self, AtomName):
rootPath = GlobalVariable.GlobalVar.getInstance().getValue("atomRootPath")
if not os.path.exists(rootPath + '/' + AtomName):
os.makedirs(rootPath + '/' + AtomName)
else:
print(AtomName + " already exists")
if not os.path.exists(rootPath + '/' + AtomName + '/' + AtomName + '.docx'):
file=Document()
file.add_paragraph(r"原子问题:"+AtomName)
file.save(rootPath + '/' + AtomName + '/' + AtomName + '.docx')
else:
print("docx already exists")
# if exist()
def showRightMenu(self):
menu = QMenu(self.treeWidget)
addAction = QAction("add",self.treeWidget)
delAction = QAction("del",self.treeWidget)
modAction = QAction("mod",self.treeWidget)
menu.addAction(addAction)
menu.addAction(delAction)
menu.addAction(modAction)
menu.exec(QCursor.pos())
if __name__ == "__main__":
import sys
gv = GlobalVariable.GlobalVar.getInstance()
_temp_path = r'D:/PythonProject/Editor/main/template.json'
gv.setValue("atomTemplate", _temp_path)
_tempRootPath = r'D:/Data/EditorRoot'
gv.setValue("atomRootPath", _tempRootPath)
app = QtWidgets.QApplication(sys.argv)
dockWidget = QtWidgets.QDockWidget()
ui = atomWindow(dockWidget)
dockWidget.show()
sys.exit(app.exec_())
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?