[PySide] signals with variable-length argument lists

345 views
Skip to first unread message

Luke Campagnola

unread,
Mar 3, 2011, 7:12:38 PM3/3/11
to pys...@lists.pyside.org
Hello,
In my PyQt app, I frequently use variable-length argument lists with my signals, like this:
    self.emit(QtCore.QObject.SIGNAL('signalName'), *args)
 
Is there any way to accomplish the same thing in PySide using new-style signals?
 
Thanks!

MuSheng

unread,
Mar 3, 2011, 9:43:36 PM3/3/11
to Luke Campagnola, pys...@lists.pyside.org
_______________________________________________ PySide mailing list PyS...@lists.pyside.org http://lists.pyside.org/listinfo/pyside

Luke Campagnola

unread,
Mar 10, 2011, 8:00:30 AM3/10/11
to MuSheng, pys...@lists.pyside.org
Thanks, but that's not quite what I'm looking for. I have slot functions that take variable argument lists, and I'd like to be able to connect signals to these. For example:

    def slotFn(arg1, arg2, *args):
        pass

    class Obj(QObject):
        sig = QSignal(...)
        def emitSig(self):
            self.sig.emit(arg1, arg2, *[args])
         
    obj = Obj()
    obj.sig.connect(slotFn)

The best I can come up with is to send a single list through the signal, as you suggest, and then unpack the list with a wrapper function:

    obj.sig.connect(lambda args: slotFn(*args))

.. but this is a little messy, and if I remember correctly, introducing lambda functions brings up some other problems (http://developer.qt.nokia.com/wiki/Differences_Between_PySide_and_PyQt)

With old-style signals/slots, this was trivial. I might write up a PSEP for it.. can anyone think of a reason it might not work? 

Luke

Luke Campagnola

unread,
Mar 10, 2011, 3:33:03 PM3/10/11
to pys...@lists.pyside.org
I want to provide another example of how I have used variable-length arguments in PyQt, in hopes that perhaps someone can suggest a workaround or another approach:

I have a class SignalProxy which is a subclass of QObject. The purpose of SignalProxy is to catch rapidly repeated signals from one source, merge them together into a single signal, and re-emit that signal. I use this in many different places, but most commonly to catch valueChanged signals from a QSpinBox, so that rolling the mouse wheel over the spin box only generates a single signal at the end, rather than a rapid series of signals.  (I have attached the code for this class for reference)

In PyQt (with old-style signals), this is trivial because when I emit a signal, I do not need to know the name or signature of arguments that will be passed through the signal until just before it is sent. Furthermore, I do not need to select a particular signature when connecting a custom signal to its slot; I simply connect it by name and _all_ signals with that name will be connected, regardless of their argument signature. 

This does not seem to be possible with PySide since I can not have arbitrary signatures and I don't know any way to create new signals on the fly (at least, none that would work in this example). 

Any ideas? I like the syntax of these new-style signals, but so far I've found them to be much more limited in their capabilties..

Luke
SignalProxy.py
Reply all
Reply to author
Forward
0 new messages