7

With the following code, I get an error ('PySide.QtCore.Signal' object has no attribute 'emit') when trying to emit a signal:

#!/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?

quazgar
  • 4,304
  • 2
  • 29
  • 41

2 Answers2

13

The problem here is that although the class correctly inherits from QtCore.QObject, it does not call the parent's constructor. 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()
quazgar
  • 4,304
  • 2
  • 29
  • 41
1

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)
Dariusz
  • 960
  • 13
  • 36
  • Why do you use the parent's name (`QObject`) explicitly instead of using `super` and what exactly is odd about the other answer? You may want to read https://stackoverflow.com/q/222877. – quazgar Nov 11 '20 at 12:14