在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_())