PySide 信号“复制”行为
from PySide.QtCore import *
class Eggs(QObject):
evt_spam = Signal()
print "Loaded"
a = Eggs()
b = Eggs()
print a.evt_spam
print b.evt_spam
print a.evt_spam is b.evt_spam
输出:
Loaded
<PySide.QtCore.Signal object at 0xa2ff1a0>
<PySide.QtCore.Signal object at 0xa2ff1b0>
False
“Loaded”仅打印一次(如预期;它是一个类变量),但为什么要创建 2 个信号实例(如果它也是一个类变量)?
from PySide.QtCore import *
class Eggs(QObject):
evt_spam = Signal()
print "Loaded"
a = Eggs()
b = Eggs()
print a.evt_spam
print b.evt_spam
print a.evt_spam is b.evt_spam
outputs:
Loaded
<PySide.QtCore.Signal object at 0xa2ff1a0>
<PySide.QtCore.Signal object at 0xa2ff1b0>
False
"Loaded" only printing once (as expected; it is a class variable), but why are 2 instances of the signal being created (if it is also a class variable)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不会在创建类实例时打印,而是在执行类作用域时打印。这段代码将打印“Loaded”,即使我从未创建过“Test”的实例。
如果您想在类初始化时运行代码,请查看
__init__()
。此代码将在创建实例时打印“Loaded”,而不是在定义类本身时打印“Loaded”。QT 的 QObject 元类似乎正在重写类属性,以防止在初始化类的新实例时出现重复信号。也许您可以像这样分配属性:
或者这样:
It's not being printed when you create a class instance, but rather when the class scope is executed. This code will print "Loaded", even though I never made an instance of "Test".
If you want to run code when the class is initialized, take a look at
__init__()
. This code will print "Loaded" when an instance is made, instead of when the class itself is defined.QT's QObject metaclass appears to be rewriting the class attributes to prevent duplicate signals when you initialize a new instance of the class. Perhaps you can assign the attribute like this:
Or this: