PySide: 'PySide.QtCore.Signal' 对象没有属性 'emit'
PySide: 'PySide.QtCore.Signal' object has no attribute 'emit'
使用以下代码,我在尝试发出信号时遇到错误 ('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()
我该怎么做才能解决这个问题?
这里的问题是,虽然class正确地继承自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()
上面的解决方案对我来说很“奇怪”...因此我在下面提供我的...
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)
使用以下代码,我在尝试发出信号时遇到错误 ('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()
我该怎么做才能解决这个问题?
这里的问题是,虽然class正确地继承自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()
上面的解决方案对我来说很“奇怪”...因此我在下面提供我的...
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)