[PyQt] QContextMenuEvent on a QTreeWidgetItem

Baz Walter bazwal at ftml.net
Fri Dec 17 17:43:01 GMT 2010


On 17/12/10 09:40, Christopher Evans wrote:
> Hi guys,
>
> I have been using pyqt for years at many different jobs and many different
> projects, however some things elude me. (I am a self-taught noob at heart)
>
> I have written many tutorials for the community to help spread knowledge,
> but this is one of the many things I myself have never found a good tutorial
> on:
>
> I just want to make a simple right click contextual menu on a
> treewidgetitem.
>
> Can anyone share a snippet showing this?

here's an example that handles context menu events invoked either by 
mouse or by keyboard. the keyboard event handler also scrolls the 
treewidgetitem into view if necessary.


from PyQt4.QtGui import (
     QApplication, QTreeWidget, QTreeWidgetItem, QMenu,
     )


class TreeWidget(QTreeWidget):
     def __init__(self, parent=None):
         QTreeWidget.__init__(self, parent)
         self.setWindowTitle('TreeWidget Context Menu')
         self.header().setHidden(True)
         self.setGeometry(320, 320, 320, 320)
         for index in '12345':
             parent = QTreeWidgetItem(self, ['Parent %s' % index])
             for item in 'ABCDE':
                 QTreeWidgetItem(parent, ['Child %s%s' % (index, item)])
             parent.setExpanded(True)

     def contextMenuEvent(self, event):
         if event.reason() == event.Mouse:
             pos = event.globalPos()
             item = self.itemAt(event.pos())
         else:
             pos = None
             selection = self.selectedItems()
             if selection:
                 item = selection[0]
             else:
                 item = self.currentItem()
                 if item is None:
                     item = self.invisibleRootItem().child(0)
             if item is not None:
                 parent = item.parent()
                 while parent is not None:
                     parent.setExpanded(True)
                     parent = parent.parent()
                 itemrect = self.visualItemRect(item)
                 portrect = self.viewport().rect()
                 if not portrect.contains(itemrect.topLeft()):
                     self.scrollToItem(
                         item, QTreeWidget.PositionAtCenter)
                     itemrect = self.visualItemRect(item)
                 itemrect.setLeft(portrect.left())
                 itemrect.setWidth(portrect.width())
                 pos = self.mapToGlobal(itemrect.center())
         if pos is not None:
             menu = QMenu(self)
             menu.addAction(item.text(0))
             menu.popup(pos)
         event.accept()


if __name__ == "__main__":

     import sys
     app = QApplication(sys.argv)
     tree = TreeWidget()
     tree.show()
     sys.exit(app.exec_())


More information about the PyQt mailing list