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

不想你离开。 提交于 2019-11-28 02:05:58

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!