Send and receive signals from another class pyqt

2024/10/11 20:27:45

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 sysclass 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.

Answer

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()
https://en.xdnf.cn/q/118284.html

Related Q&A

I can not add many values in one function

I have a gui applicationI put text into text box1, text box2,………… text box70 ,and then click on the pushButton, The function return_text () in the module_b.py be called. Now I can call one instance…

Close browser popup in Selenium Python

I am scraping a page using Selenium, Python. On opening the page one Popup appears. I want to close this popup anyway. I tried as below:url = https://shopping.rochebros.com/shop/categories/37browser = …

How can I replace certain string in a string in Python?

I am trying to write two procedures to replace matched strings in a string in python. And I have to write two procedures. def matched_case(old new): .........note: inputs are two strings, it returns a…

Python: `paste multiple (unknown) csvs together

What I am essentially looking for is the `paste command in bash, but in Python2. Suppose I have a csv file:a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3And another such:e1,f1 e2,f2 e3,f3I want to pull them toget…

Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter page. But this only works when the login is successful. How can i do the same thing when some error occurs?? Ps: I…

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…