使用python右键单击QTreeWidget上的弹出菜单

时间:2013-12-14 10:30:05

标签: python popup mouseclick-event qtreewidget qtreewidgetitem

我正在尝试编写一个包含treewidget的python程序,并且能够在用鼠标右键单击后添加,重命名和删除集群。 我对python很新,到目前为止这是我的代码:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.treeWidget = QtGui.QTreeWidget(self.centralwidget)
    self.treeWidget.setGeometry(QtCore.QRect(155, 50, 481, 361))
    self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    self.buildingTree()
    QtCore.QMetaObject.connectSlotsByName(MainWindow)


def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    self.treeWidget.headerItem().setText(0, _translate("MainWindow", "Assignment1", None))
    __sortingEnabled = self.treeWidget.isSortingEnabled()


def buildingTree(self):
    item_0 = QtGui.QTreeWidgetItem(self.treeWidget)
    item_1 = QtGui.QTreeWidgetItem(item_0)
    item_2 = QtGui.QTreeWidgetItem(item_1)
    item_1 = QtGui.QTreeWidgetItem(item_0)
    item_2 = QtGui.QTreeWidgetItem(item_1)
    self.treeWidget.setSortingEnabled(False)
    self.treeWidget.topLevelItem(0).setText(0, _translate("MainWindow", "default", None))
    self.treeWidget.topLevelItem(0).child(0).setText(0, _translate("MainWindow", "cluster1", None))
    self.treeWidget.topLevelItem(0).child(0).child(0).setText(0, _translate("MainWindow", "clusterA", None))
    self.treeWidget.topLevelItem(0).child(1).setText(0, _translate("MainWindow", "cluster2", None))
    self.treeWidget.topLevelItem(0).child(1).child(0).setText(0, _translate("MainWindow", "clusterA", None))



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

有人可以告诉我如何在某个群集上单击鼠标右键后打开弹出菜单? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了答案。 它看起来像这样:

def new_cluster(self):
    print "New Cluster"

def rename_cluster(self):
    print "Rename cluster"

def delete_cluster(self):
    print "Delete cluster"

def create_popup_menu(self, parent=None):
    self.popup_menu = QtGui.QMenu(parent)
    self.popup_menu.addAction("New", self.new_cluster)
    self.popup_menu.addAction("Rename", self.rename_cluster)
    self.popup_menu.addSeparator()
    self.popup_menu.addAction("Delete", self.delete_cluster)

def on_context_menu(self, pos):        
    node = self.treeWidget.mapToGlobal(pos)
    self.popup_menu.exec_(self.treeWidget.mapToGlobal(pos))