Passing an argument when starting new QThread() in PyQt

后端 未结 2 608
不知归路
不知归路 2021-01-01 04:29

I have a multi-threaded application written in Python in which one thread \"takes care\" of the GUI, and the other is the worker thread. However, the worker thread has two m

2条回答
  •  一向
    一向 (楼主)
    2021-01-01 05:06

    The start method of QThread doesn't accept arguments. However, you've inherited QThread so you're free to customize it at will. So, to implement what you want, just pass arguments into the constructor of Worker.

    Here's your code sample slightly modified to show this in action:

    class Worker(QThread):
      def __init__(self, do_create_data=True, parent=None):
        super(QThread, self).__init__()
        self.do_create_data = create_data
    
      def run(self):
         if self.create_data:
             self.create_data()
         else:
             self.upload_data(), depends
    

提交回复
热议问题