PySide 声称Signals can be defined using the QtCore.Signal() class. Python types and C types can be passed as parameters to it. If you need to overload it just pass the types as tuples or lists
. 从 PySide 中的 PySide Doc Signals and Slots 中,他们展示了一种同时创建多个信号的方法。那条线是:
# create two new signals on the fly: one will handle
# int type, the other will handle strings
speak = QtCore.Signal((int,), (str,))
我走了一点,创造了这样的东西(用于学习目的):
speak = QtCore.Signal((int,), (str,), (tuple,), (list,), (A,), (B,), (Exception,), (unicode,), (float,))
A
并且B
是我创建的两个不同的虚拟新样式类。然后我通过打印它们来调查实际创建了多少信号:
print someone.speak
print someone.speak[int]
print someone.speak[str]
print someone.speak[tuple]
print someone.speak[list]
print someone.speak[A]
print someone.speak[B]
print someone.speak[Exception]
print someone.speak[unicode]
print someone.speak[float]
我得到了这些:
<PySide.QtCore.SignalInstance object at 0x02179BA0>
<PySide.QtCore.SignalInstance object at 0x02179BA0>
<PySide.QtCore.SignalInstance object at 0x02179BB0>
<PySide.QtCore.SignalInstance object at 0x02179BC0>
<PySide.QtCore.SignalInstance object at 0x02179BC0>
<PySide.QtCore.SignalInstance object at 0x02179BC0>
<PySide.QtCore.SignalInstance object at 0x02179BC0>
<PySide.QtCore.SignalInstance object at 0x02179BC0>
<PySide.QtCore.SignalInstance object at 0x02179BB0>
<PySide.QtCore.SignalInstance object at 0x02179C00>
Obersevation: tuple
, list
,Exception
和我的习惯A
并B
得到相同的信号。我知道元组和列表有点笼统,被 PySide 用来重载东西。但为什么我的A
和B
也得到相同的信号?看起来只有那些真正原始的类型,如int
,float
并且str
在 Python 中获得新信号。
任何人都可以解释这种奇怪的行为吗?
提前非常感谢!
[更新]
当我在一个真正的 PySide 项目中遇到问题时,我做了上述探索。我将真实版本简化为:
#!/usr/bin/env python
import sys
from PySide import QtCore
class A(object):
def __init__(self, msg):
self.msg = msg
class B(object):
def __init__(self, msg):
self.msg = msg
@QtCore.Slot(A)
def sayA(a):
print 'From sayA:', a.msg
@QtCore.Slot(B)
def sayB(b):
print 'From sayB:', b.msg
class Communicate(QtCore.QObject):
speak = QtCore.Signal((A,), (B,))
someone = Communicate()
someone.speak[A].connect(sayA)
someone.speak[B].connect(sayB)
someone.speak[A].emit(A('A is good'))
someone.speak[B].emit(B('B is bad'))
它将打印出:
From sayA: A is good
From sayB: A is good
From sayA: B is bad
From sayB: B is bad
我预计A is good
只能从 sayA 打印。