So, I’m writing an asset manager and am using a QTreeWidget as part of my display. I need to pop up menu’s when the user right clicks on the tree widget but I’m completely dead ending on retrieving the info on the mouse button click.
I can pass the signal to get the itemClicked() or itemPressed() event but what I need is what mouse button was pushed and I just can’t find that.
At the moment I’m about to inherit QTreeWidget and embed the information in the object with event handlers but I have a hard time believing this information isn’t somewhere accessible by default and I just can’t find it.
I’m not sure of the correct way to do this, but based on the menus.pyw example that comes with PyQt4 you could do this.
In the contextMenuEvent of the MainWindow, check the position to see if it is within the tree widget.
def contextMenuEvent(self, event):
# The menu
menu = QtGui.QMenu(self)
# Add your actions
menu.addAction(self.actAddFiles)
menu.addAction(self.actResetFiles)
# Add some check here for event.globalPos() to see if it's in your widget
# Open the menu
menu.exec_(event.globalPos())
Ooops, the QTreeWidget should have its own “contextMeneEvent” so you wouldn’t have to check the position. In that method create the QMenu you want to open. The event is triggered when the user right clicks the widget.
How you display your menu will depend on how you’ve implemented it.
I think "customContextMenuRequested(QPoint) will return a relative screen QPos based on the listWidget’s parent.
Another way to get the position is via QtGui.QCursor.pos()
this is an example menu implementation
self.MyMenu= QtGui.QMenu(“CustomMenu”)
templateInspect = self.TemplateMenu.addAction(“Inspect Current”)
templateInspect.setToolTip(“Inspect the current tab template”)
self.connect(templateInspect, QtCore.SIGNAL(“triggered()”), self._InspectCurrent)
Hope this helps.
I wrote a large AssetBrowser for our pipeline here. In Maya with no debugging (PyQt) and in 2009 (plus it was my 1st PyQT project). This not the best way to develop a large gui. (In Maya anyway). I had no end of fatal crashes and then having to comment out lines of code to narrow down the bug. I would recommend doing large projects in C++ and not PyQt. That way you have more of a chance to catch errors / bugs.