[PySide] Binding unbound signals (need __get__)

Blair Zajac blair at orcaware.com
Fri May 11 03:06:17 CEST 2012


We have some Python decorators that takes signals and we'd like to be 
able to convert a unbound signal into a bound signal.  I don't see a way 
to do this in PySide, as in PyQt4 __get__ works to bind an unbound 
signal.  From my reading, normal python usage lets you bind any method 
using __get__:

class Bar(object):
     def method(self):
         pass

bar = Bar()
unbound_method = Bar.method
bound_method = Bar.method.__get__(bar)


Without __get__, the following doesn't work.  Is there a way to do this 
in PySide?

Thanks,
Blair


from PyQt4 import QtCore
QtCore.Signal = QtCore.pyqtSignal
QtCore.Slot = QtCore.pyqtSlot

def emit_upon_success(signal):
     def f_(f):
         def f__(self):
             result = f(self)
             s = signal.__get__(self)
             s.emit()
             return result
         return f__
     return f_

class Foo(QtCore.QObject):
     SIG = QtCore.Signal()

     @emit_upon_success(SIG)
     def do_something(self):
         pass

foo = Foo()
foo.do_something()




More information about the PySide mailing list