I have an unusual case of needing to pass an extra argument when defining a connection to a customContextMenuRequested
slot in PyQt.
Of course, the basic command is usually done in the form of:
myListWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
myListWidget.customContextMenuRequested.connect( self.Whatever_Popup )
I'm also aware and often use both the lambda and functools.partial
methods to pass extra arguments to other general functions.
However, I have a case where I need to pass a QObject
(listWidget
in this case) when defining the connection.
The receiving function is structured like so:
def Whatever_Popup(self, pos):
globalPos = self.mapToGlobal(pos)
globalPos.setX( globalPos.x() + 720)
globalPos.setY( globalPos.y() + 115)
menu = QtGui.QMenu()
menu.addAction("Edit")
selectedItem = menu.exec_(globalPos)
if selectedItem:
if selectedItem.text() == "Edit":
print "Here"
#self.DoEdit(widget)
I've tried all the following but with no joy.
myListWidget.customContextMenuRequested.connect( partial(self.Whatever_Popup, newLst))
myListWidget.customContextMenuRequested.connect( partial(self.Whatever_Popup, QtCore.QPoint, newLst))
myListWidget.customContextMenuRequested.connect( partial(self.Whatever_Popup, QtCore.SIGNAL("customContextMenuRequested(const QPoint &)"), newLst))
myListWidget.customContextMenuRequested.connect( lambda: self.Whatever_Popup, (QtCore.SIGNAL("customContextMenuRequested(const QPoint &)"), newLst) )
All return an error. Am I close? or is this perhaps not possible? which would be a bit surprising.
myListWidget
is generated dynamically btw. I need to pass some reference
to the listWidget
because self.DoEdit(widget)
will need it for further processing. Can't pre-define the name and retrieve it in self.DoEdit
since it's created dynamically.
Does anyone have a solution to this?
Thanks to all in advance,