[PyQt] context menus for QTreeWidgetItem?

projetmbc projetmbc at club-internet.fr
Thu Apr 9 08:25:02 BST 2009


Christian Aubert a écrit :
> I currently have a context menu for the whole QTreeWidget but I need 
> finer-grained control on a per item basis. Has anyone implemented 
> context menus for QTreeWidgetItems? Any pointers?
>
> Christian
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Hello,
here is a solution that works. I hope tha will help you.

To call the menu, just use :
        self.connect(self.treeWidget, 
QtCore.SIGNAL("customContextMenuRequested(const QPoint &)"), 
self.menuContextTree)

Here is the method menuContextTree :
       def menuContextTree(self, point):
# Infos about the node selected.
           index = self.treeWidget.indexAt(point)

           if not index.isValid():
              return

           item = self.treeWidget.itemAt(point)
           name = item.text(0)  # The text of the node.

# We build the menu.
           menu=QtGui.QMenu(self)
           action=menu.addAction("Souris au-dessus de")
           action=menu.addAction(name)
           menu.addSeparator()
           action_1=menu.addAction("Choix 1")
           action_2=menu.addAction("Choix 2")
           action_3=menu.addAction("Choix 3")

           print QtGui.QCursor.pos()
           menu.exec_(QtGui.QCursor.pos())



More information about the PyQt mailing list