What you want to do will work just fine if you use functools.partial

#---
from functools import partial
...
btn.clicked.connect(partial(self.doStuff, btn))
#---

Or you can use the self.sender() call to find out who emitted the signal :

#----
def doStuff(self):
    btn = self.sender()

btn.clicked.connect(self.doStuff)
#----

You have to be a bit aware of the nuisances of using partial. Normally when
you connect a signal from one object to the method of another, and the slot
object gets deleted, Qt will clean up the connection for you. But if you
wrap it in partial, then Qt only sees a function object and doesn't clean
it up when the object it wraps is deleted. Its not a problem for things
like this where all your buttons live on the parent and all stay together
for the same life cycle. But for something where you create temporary
objects and connect signals, partial can make them stay connected and still
fire on a deleted reference.
I've also read that the same situation can cause a memory leak, since the
partial is holding on to the reference of an object, and the signal slot is
not being cleaned up, the object reference   never hits zero so it stays
around.


On May 4, 2014 7:01 AM, "mAtt RINGOT" <[email protected]> wrote:
>
> Hey guys,
>
> It works really nicely but now I would like to have the contextMenu only
on some of the buttons, not all of them, can I do that from a list of
string ?... And also, how can I connect buttons from a list of string to
the same command and passing the button Name as an argument ?
>
> pretty much like (in a perfect and too easy world ahah) :
>
>         buttonList= [self.ui.buttonA, self.ui.buttonB, self.ui.buttonC]
>         for btn in buttonList:
>             btn.clicked.connect(self.doStuff(btn))
>
> Thanks a lot for your time !
>
> m.
>
> Le samedi 3 mai 2014 12:58:42 UTC+2, mAtt RINGOT a écrit :
>>
>> Waow, thanks a lot for your quick reply !!
>>
>> I will have a try !
>>
>> Thanks a lot, I let you know !
>>
>> Le samedi 3 mai 2014 10:47:01 UTC+2, Manuel Macha a écrit :
>>>
>>> Alternatively you could sub-class QPushButton,
>>> implement the context-menu and create your buttons from the sub-class
instead of from QPushButton.
>>>
>>>
>>> On Sat, May 3, 2014 at 8:50 AM, Justin Israel <[email protected]>
wrote:
>>>>
>>>> If you have the exact same menu to show for all the buttons then you
could just handle the menu from the window instead of individually for each
button:
>>>>
>>>> class Window(QtGui.QDialog):
>>>>
>>>>     def __init__(self):
>>>>         super(Window, self).__init__()
>>>>         self.resize(800,600)
>>>>
>>>>         layout = QtGui.QVBoxLayout(self)
>>>>         for i in xrange(10):
>>>>             button = QtGui.QPushButton("Button %d" % i, self)
>>>>             layout.addWidget(button)
>>>>
>>>>     def contextMenuEvent(self, event):
>>>>         child = self.childAt(event.pos())
>>>>         if isinstance(child, QtGui.QPushButton):
>>>>             event.accept()
>>>>             self._showButtonMenu(child, event.globalPos())
>>>>             return
>>>>
>>>>         super(Window, self).contextMenuEvent(event)
>>>>
>>>>     def _showButtonMenu(self, button, pos):
>>>>         menu = QtGui.QMenu()
>>>>         menu.addAction('First Action')
>>>>         menu.addAction('Second Action')
>>>>         menu.exec_(pos)
>>>>
>>>>
>>>>
>>>>
>>>> On Sat, May 3, 2014 at 9:20 AM, mAtt RINGOT <[email protected]> wrote:
>>>>>
>>>>> Hey guys,
>>>>>
>>>>> I hope you are well !
>>>>>
>>>>> I'm currently trying to create quite a lot of contextMenu for lots of
different buttons in a maya UI I'm working on.
>>>>>
>>>>> The issue is that I've a long list of buttons and I need the same
contextMenu for all of them.... What I'm doing now is not really elegant,
that's why I'm asking if you could help me...
>>>>>
>>>>> Because with the following process, my script will be reallllyyy
realllyyy long but I can't figure out how to clean that properly :(
>>>>>
>>>>> Here's an example of my code :
>>>>>
>>>>> from PyQt4 import QtCore,QtGui,uic
>>>>> from functools import partial
>>>>> import os
>>>>>
>>>>>
>>>>> class MyWidget(QtGui.QWidget):
>>>>> def __init__(self, parent=None):
>>>>> QtGui.QWidget.__init__(self, parent)
>>>>>
>>>>> uiFilePath = os.path.join(os.path.dirname(__file__),"myfile.ui")
>>>>> self.ui = uic.loadUi(uiFilePath, self)
>>>>> '''
>>>>> Right Click Menu
>>>>> '''
>>>>> self.ui.buttonA.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
>>>>> self.ui.buttonA.customContextMenuRequested.connect(self.buttonAMenu)
>>>>> self.ui.buttonB.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
>>>>> self.ui.buttonB.customContextMenuRequested.connect(self.buttonBMenu)
>>>>> '''
>>>>> BUTTON A
>>>>> '''
>>>>> @QtCore.pyqtSlot()
>>>>> def on_buttonA_released(self):
>>>>> print ('Doing Stuff when clicking on Button A')
>>>>> def buttonAMenu(self, pos):
>>>>> menu = QtGui.QMenu()
>>>>> menu.addAction('First Action',
lambda:self.FirstActionButtonA(objects))
>>>>> menu.addAction('Second Action',
lambda:self.SecondActionButtonA(objects))
>>>>> menu.exec_(QtGui.QCursor.pos())
>>>>> def FirstActionButtonA(self, objects):
>>>>> print ('First Action working on :')
>>>>> print (objects)
>>>>> def SecondActionButtonA(self):
>>>>> print ('Second Action working on :')
>>>>> print (objects)
>>>>> '''
>>>>> BUTTON B
>>>>> '''
>>>>> @QtCore.pyqtSlot()
>>>>> def on_buttonB_released(self):
>>>>> print ('Doing Stuff when clicking on Button B')
>>>>> def buttonBMenu(self, pos):
>>>>> menu = QtGui.QMenu()
>>>>> menu.addAction('First Action',
lambda:self.FirstActionButtonB(objects))
>>>>> menu.addAction('Second Action',
lambda:self.SecondActionButtonB(objects))
>>>>> menu.exec_(QtGui.QCursor.pos())
>>>>> def FirstActionButtonB(self, objects):
>>>>> print ('First Action working on :')
>>>>> print (objects)
>>>>> def SecondActionButtonB(self):
>>>>> print ('Second Action working on :')
>>>>> print (objects)
>>>>>
>>>>>
>>>>> Sorry, the code highlighting seems to not work...
>>>>>
>>>>> Thanks a lot for your help !
>>>>>
>>>>> m.
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
Groups "Python Programming for Autodesk Maya" group.
>>>>> To unsubscribe from this group and stop receiving emails from it,
send an email to [email protected].
>>>>> To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/cd18e5e1-8b14-4e74-9017-9dac18a6c69a%40googlegroups.com
.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected].
>>>> To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0gZZPBoLV-N66t_62oAhvCjgiFPoczJHr_QHBvh57WXA%40mail.gmail.com
.
>>>>
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
> --
> You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
> To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/02e91e83-ca91-4f41-880f-ba4448233210%40googlegroups.com
.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0tCwhG%3DFTPgmZrZ0d5%3DaGf9PsrZ00CHSehXMkJmyr6Kg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to