Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to emit a signal from another Class Method?
#1
Hi,

I got the emit signal working when it is in the Worker class - def run(self) method. Everything runs fine, the while loop is able to cycle and emit a signal every 1 second. Then the label will be updated after receiving the signal.

I decided to try out by placing the while loop in another class named loop, methodA. This is to try see if the emitted signal will be picked up by the MainWindow. Unfortunately, the signal is not picked up and the program hung up thereafter.

Did I miss any step that prevented the while loop's emitting signals from being picked up? Kindly point me in the right direction by making changes to my script.

Thanks.
import sys
import time
from PyQt5 import QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal
from mydialog import Ui_mydialog


class Mainwindow(QtWidgets.QMainWindow, Ui_mydialog):

    def __init__(self, *args, obj=None, **kwargs):
        super(Mainwindow, self).__init__(*args, **kwargs)
        self.setupUi(self)

        self.thread = Worker()
        self.loop = loop()
        self.thread.sig.connect(self.updatelabel)

        self.mypushbutton.clicked.connect(self.mypushbuttonclicked)


    def mypushbuttonclicked(self):
        self.thread.start()

    def updatelabel(self, text):
        self.mylabel.setText(text)

class Worker(QThread):

    sig = pyqtSignal(str)

    def __init__(self, parent=None):
        super(Worker, self).__init__(parent)
        # self.count = 0
        self.loop = loop()

    def run(self):

        self.loop.methodA()

        ## Original code without being in class loop and method loopA
        # while True:
        #     time.sleep(1)
        #     self.count += 1
        #     if (self.count % 1 == 0):
        #         self.sig.emit(f"Timer: {self.count} s")

# Newly added class with method "methodA"
class loop(object):

    sig = pyqtSignal(str)

    def __init__(self):
        self.count = 0

    def methodA(self):

        while True:
            time.sleep(1)
            self.count += 1
            if (self.count % 1 == 0):
                self.sig.emit(f"Timer: {self.count} s")



app = QtWidgets.QApplication(sys.argv)
window = Mainwindow()
app.setStyle("Fusion")
window.show()
app.exec()
Quote
#2
Code shown is not able to be run, from mydialog import Ui_mydialog is missing.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Return boolean from recursive class method medatib531 6 98 Jul-13-2020, 04:27 AM
Last Post: medatib531
  referencing another method in a class Skaperen 6 274 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  calling on a method from one class into another class which is not a child NABA 5 228 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  threading for method outside class anna 0 202 Nov-17-2019, 07:05 AM
Last Post: anna
  how can I changing a base class method voidptr 2 266 Nov-10-2019, 10:53 PM
Last Post: voidptr
  How to Call a method of class having no argument dataplumber 7 835 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  ignoring a signal Skaperen 4 489 Sep-27-2019, 07:14 PM
Last Post: Skaperen
  Call method from another method within a class anteboy65 3 526 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  problem with class method AmirAB 3 703 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  why my method doesn't find my List in the same class? Scorpio 2 565 Jan-31-2019, 05:21 PM
Last Post: Scorpio

Forum Jump:


Users browsing this thread: 1 Guest(s)