简体   繁体   中英

How to send signal from inside of imported function on pyqt5?

I have one Worker Thread on my program that returns a number every second, forever, until the stop signal is emmited on the Main Window. I want it to emit a signal everytime it changes a value, so I can update the Main Window with the returned value. I tried simply emmiting a signal in the function itself, but doesn't work.

Worker:

from test import save_data

class Worker(QObject):
    finished = pyqtSignal()  # give worker class a finished signal

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

    def do_work(self):
        save_data.main()
        self.finished.emit()  # emit the finished signal when the loop is done

    def stop(self):
        save_data.run = False  # set the run condition to false on stop

test.py:

import time
from Pyqt5.QtCore import pyqtSignal

run = True
value = pyqtSignal(int)

def main():
    x = 1
    while run:
        print(x)
        x += 1
        time.sleep(1)
        #value.emit(x)

Final code:

from PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlot, Qt, QTimer
import time



class Main(QObject):

    divisible_by_4 = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)
        self.run = True

    def main(self):
        x = 1
        while self.run:

            print(x)
            x += 1

            if x%4 == 0:
                self.divisible_by_4.emit()

            time.sleep(1)

    def stop(self):
        self.run = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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