Darcyys
Darcyys

Reputation: 179

Send and receive signals from another class pyqt

I am needing a way to receive signals sent by a Class to another class. I have 2 classes: In my first class I have a function that emits a signal called 'asignal' In my second class I call the first class function, and it emits a signal. but I can't connect the first class signal to my pushbutton. How I can do that?

I get this error: AttributeError: 'QPushButton' object has no attribute 'asignal'

from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import *
import sys


class Signals(QObject):
    asignal = pyqtSignal(str)

    def __init__(self):
        super(Signals, self).__init__()
        self.do_something()

    def do_something(self):
        self.asignal.emit('Hi, im a signal')


class Main(QWidget):
    def __init__(self):
        super(Main, self).__init__()
        self.setGeometry(300, 250, 400, 300)
        self.show()
        self.coso()

    def coso(self):
        btn = QPushButton('click me')
        btn.asignal.connect(lambda sig: print("Signal recieved" + sig))
        s = Signals()
        s.do_something()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Main()
    app.exec_()

I want a way to receive the signal emitted from my 'Signals' class to my 'Main' class. And then, in my main class, connect the signal to a Widget.

Upvotes: 0

Views: 4516

Answers (1)

RvdK
RvdK

Reputation: 19800

QPushButton is not a Signals object, so thats why you get the error.

s = Signals()
s.asignal.connect(lambda sig: print("Signal recieved" + sig))
s.do_something()

Upvotes: 3

Related Questions