popup menu on QTreeWidget on right mouse click with python

4.3k views Asked by At

I'm trying to write a program with python that contains a treewidget and having the ability to add, rename and remove cluster after clicking with the right mouse button on them. I'm quite new with python and this is my code so far:

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

Could someone please tell me how do I open a popup menu after clicking with the right mouse button on a certain cluster? Thank you in advance!

1

There are 1 answers

0
itzick binder On

I have found an answer. It looks like this:

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))