Search code examples
pythonpyqt5qthread

PyQt5 Emit signal from another module


Been searching for solution for over week now and still don't get it. I have went over YouTube tutorials, PyQt5 documentation and stackoverflow cases, but there is described how to use signals and slot in other way I want to achieve. Even after following the recommendations of more experienced programmers I can't call emit signal from another module, but I can emit from same module.

Convert_main function based on given input (list of 10-20 numbers - self.window.input_line.text()) perform loop for each number from the list. At the end of each loop I want to update progress bar so user will know the progress of the main code. I can only emit signal within ThreadClass class and can't emit from convert_main function (comments in code for reference). What I am doing wrong :( How I should rearrange my code so it will meet my needs?

At debugger state I don't receive any error and code outcome is accomplished but progress_bar is still not updating.

#*****ui_main.py module****

import sys
import convert
import db_analysis
import qdarkstyle
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QThread
from PyQt5 import QtCore, uic

Ui_MainWindow, QtBaseClass = uic.loadUiType('interface.ui')


class Window(QtBaseClass, Ui_MainWindow):

    def __init__(self, parent=None):

        super(QtBaseClass, self).__init__(parent)

        self.setupUi(self)
        self.worker = ThreadClass(self)

        self.worker.signal.connect(self.update_progress_bar)  # connecting signal to slot

        self.push_import.clicked.connect(self.import_data)
        self.push_cancel.clicked.connect(self.close_app)
        self.push_ok.clicked.connect(self.process_input)

    def update_progress_bar(self, progress_val):
        self.input_progress_bar.setValue(progress_val)

    def import_data(self):
        db_analysis.insert_data()

    def process_input(self):
        self.worker.start()


class ThreadClass(QThread):
    signal = QtCore.pyqtSignal(int)

    def __init__(self, window, parent=None):
        super(ThreadClass, self).__init__(parent)
        self.window = window

    def run(self):
        self.signal.emit(20) #<-- that's working
        convert.convert_main(self, self.window.input_line.text()) #<--passing ThreadClass self to convert.py so I can emit signal from convert_main


def main():
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    window = Window()
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

#*****convert.py module********

def convert_main(worker, input_array):

    worker = worker.signal

    my_list = list(map(int, input_array.split(" ")))

    for a in my_list:
        #some loop code
        worker.emit(my_list.index(a)/len(my_list)) #<-- that's not working

Solution

  • The signal is emitted but the value that is emitted is zero since:

    0 <= my_list.index(element) < len(my_list)
    

    So

    0 <= my_list.index(element)/len(my_list) < 1.0 
    

    And how are you signaling that the signal emit integers this will approach the value of 0. So the solution is to scale it to a value of 0 to 100:

    def convert_main(worker, input_array):
        worker = worker.signal
        my_list = list(map(int, input_array.split(" ")))
        for i, a in enumerate(my_list):
            #some loop code
            worker.emit(100*(i+1)/len(my_list))