[PyQt] context menus for QTreeWidgetItem?

Christian Aubert chris at infixlabs.com
Thu Apr 9 22:19:01 BST 2009


I had this from the previous contextMenu that covered the whole treeWidget:
self.treeWidget.setContextMenuPolicy(Qt.ActionsContextMenu)

After I changed the line to this (makes sense), everything's all good:
self.treeWidget.setContextMenuPolicy(Qt.CustomContextMenu)

Thanks to everyone who replied.

Christian


projetmbc wrote:
> 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