Why does PySide implicitely create object members from class members for Signals?

They are not copies. If you check the type of those, you’ll see that the class attribute is PySide.QtCore.Signal and the instance attribute is PySide.QtCore.SignalInstance.

print "type(obj1.sig): {}".format(type(obj1.sig))
print "type(obj2.sig): {}".format(type(obj2.sig))
print "type(Klass.sig): {}".format(type(Klass.sig))

# type(obj1.sig): <type 'PySide.QtCore.SignalInstance'>
# type(obj2.sig): <type 'PySide.QtCore.SignalInstance'>
# type(Klass.sig): <type 'PySide.QtCore.Signal'>

This is necessary because of the way Qt defines signals. Qt uses Meta-Object System to register signals/slots. In order to make this work, PySide does some ‘magic’ behind the curtain to register your custom class-attribute signal with Meta-Object System and return you a usable signal (SignalInstance) with the same name as instance attribute.

Original Signal is still there, but rather overridden with the instance attribute:

print "obj1.sig -> type: {}, id: {}".format(type(obj1.sig), id(obj1.sig))
print "obj1.__class__.sig -> type: {}, id: {}".format(type(obj1.__class__.sig), id(obj1.__class__.sig))
print "Klass.sig -> type: {}, id: {}".format(type(Klass.sig), id(Klass.sig))

# obj1.sig -> type: <type 'PySide.QtCore.SignalInstance'>, id: 40629904
# obj1.__class__.sig -> type: <type 'PySide.QtCore.Signal'>, id: 41556352
# Klass.sig -> type: <type 'PySide.QtCore.Signal'>, id: 41556352

Leave a Comment