[PyQt] Context menu on items in QTreeView

Lukas LuHe at gmx.at
Tue Oct 14 21:45:01 BST 2008


Am Dienstag 14 Oktober 2008 22:30:03 schrieb Filip Gruszczyński:
> Hello,
>
> I would like to pop up a menu on items in QTreeView. I decided to
> intercept pressed(const QModelIndex&) signal, because contextMenuEvent
> doesn't provide information about the tree view chosen index. 

Not completly true, you can get the item with the function 
QTreeView.indexAt(pos)

> However,
> inside the slot for this signal I cannot acces position of the mouse
> (no event) and decide, where to pop this menu. I tried browsing
> QApplication class, but found no information about the global mouse
> position (though there is global information about buttons pressed).
> Is there anyway to obtain it and popup menu in the right place?

Here is a small example:

    def showCustomContextMenu(self, pos):
        index = self.treeview.indexAt(pos)

        if not index.isValid():
            return

        item = self.treeview.itemAt(pos)
        name = item.text(0)

        menu = QMenu(self)

       # your menu...

        menu.popup(QCursor.pos())




More information about the PyQt mailing list