使用以下代码,我在尝试发出信号时收到错误('PySide.QtCore.Signal' object has no attribute 'emit'
):
#!/usr/bin/env python
from PySide import QtCore
class TestSignalClass(QtCore.QObject):
somesignal = QtCore.Signal()
def speak_me(self):
self.speak.emit()
def __init__(self):
try:
self.somesignal.emit()
except Exception as e:
print("__init__:")
print(e)
t = TestSignalClass()
我该怎么做才能解决这个问题?
答案 0 :(得分:6)
这里的问题是虽然类正确地继承自QtCore.QObject
,但它不会调用父的构造函数。这个版本工作正常:
#!/usr/bin/env python
from PySide import QtCore
class TestSignalClass(QtCore.QObject):
somesignal = QtCore.Signal()
def speak_me(self):
self.speak.emit()
def __init__(self):
# Don't forget super(...)!
super(TestSignalClass, self).__init__()
try:
self.somesignal.emit()
except Exception as e:
print("__init__:")
print(e)
t = TestSignalClass()
答案 1 :(得分:0)
上面的解决方案对我来说是“奇怪” ...因此,我在下面提供了我的解决方案...
from PySide2.QtCore import Signal, QObject
class myTestObject(QObject):
someSignal = Signal(str)
def __init__(self):
QObject.__init__(self) # call to initialize properly
self.someSignal.connect(self.testSignal) # test connect
self.someSignal.emit("Wowz") # test
def testSignal(self, arg):
print("my signal test from init fire", arg)