简体   繁体   English

PySide:'PySide.QtCore.Signal' 对象没有属性 'emit'

[英]PySide: 'PySide.QtCore.Signal' object has no attribute 'emit'

With the following code, I get an error ( 'PySide.QtCore.Signal' object has no attribute 'emit' ) when trying to emit a signal:使用以下代码,我在尝试发出信号时收到错误( '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()

What can I do to fix this?我能做些什么来解决这个问题?

The problem here is that although the class correctly inherits from QtCore.QObject , it does not call the parent's constructor.这里的问题是,尽管该类正确地从QtCore.QObject继承,但它并没有调用父级的构造函数。 This version works fine:这个版本工作正常:

#!/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()

The solution above is "odd" to me... thus I'm providing mine below...上面的解决方案对我来说是“奇怪的”......因此我在下面提供我的......

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM