Search code examples
pythonpyqt5qthread

data transfer problem between qthread and ui?


I succeeded in creating a gui crawling program using Beautiful Soup and PyQt5.

By the way, I had a problem with gui freeze while the program executed the repeating statement. So I'm going to use QThread.

But when I bring the elements related to gui on Thread, there is a problem. (There's no problem with operating code that has nothing to do with gui, so I don't think there's any data transmission between classes.) (Is this right?)

I've created a simple problem. ↓

Program Execution Screen

import sys
import time
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


form_class = uic.loadUiType('aaaa.ui')[0]


class Thread1(QThread):
def __init__(self, Main):
    super().__init__(Main)

def run(self):
    i = 1
    while i <= 10:
        print(self.lineEdit.text().strip()) #No data transmission between Main and Thread1??
        time.sleep(1)
        i += 1


class Main(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initSetting()
        self.initSignal()


    def initSetting(self):
        self.statusBar().showMessage('Wait')
        self.setWindowTitle('aaaa')


    def initSignal(self):
        self.pushButton.clicked.connect(self.printWord)


    def printWord(self):
        self.statusBar().showMessage('Threading')

        x = Thread1(self)
        x.start()



if __name__ == "__main__" :

    app = QApplication(sys.argv)
    aaaa = Main()
    aaaa.show()
    app.exec_()

Solution

  • Using QThread, from what I understand, transmission of information between classes is doable, but to transmit information from your Main class to your Thread1 class, you have to pass arguments upon instantiation of your Thread1 class from your Main class.

    In other words, your Main class and your Thread1 class do not share any variables or functions. They are separate classes.

    Here's how I might do this:

    class Main(QMainWindow, form_class):
        def __init__(self):
            super().__init__()
            self.setupUi(self)
            self.initSetting()
            self.initSignal()
    
        def initSetting(self):
            self.statusBar().showMessage("Wait")
            self.setWindowTitle("aaaa")
    
        def initSignal(self):
            self.pushButton.clicked.connect(self.printWord)
    
        def printWord(self):
            self.statusBar().showMessage("Threading")
            some_message = self.lineEdit.text().strip()
            self.thread_1 = Thread1(some_message)
            self.thread_1.start()
    
    
    class Thread1(QThread):
        def __init__(self, input_message, parent=None):
            QThread.__init__(self, parent)
            self.input_message = input_message
    
        def run(self):
            i = 1
            while i <= 10:
                print(self.input_message)
                time.sleep(1)
                i += 1
    
    

    Let me know if this is helpful.