在treewidget中添加鼠标右击响应,比如添加树节点,删除树节点,编辑数节点的名字

36 篇文章 1 订阅

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值