Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an argument when starting new QThread() in PyQt

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 main functions (or so to say two main jobs), and I need to tell the run function which job exactly to do.

So what I had in mind was to create a run function in the worker thread which will take one parameter (save for "self). The parameter will either be "create" or upload. Without further ado, here's the somewhat-code that I have so far:

GUI.py

class GUI(QMainWindow):
  def __init__(self, parent=None):
    super, etc
    self.worker = worker.Worker()

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

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

Worker.py

class Worker(QThread):
  def __init__(self, parent=None):
    super, etc

  def run(self):
     self.create_data() # OR   self.upload_data(), depends

So the question is, how can I tell worker.start() which function I want it to perform? I realize one could directly use worker.run() method, but I was told by the "Rapid GUI development with PyQT" never to call worker.run() directly, and always to use worker.start().

like image 546
Bo Milanovich Avatar asked Oct 23 '11 06:10

Bo Milanovich


People also ask

What is QThread in pyqt5?

In PyQt, you use QThread to create and manage worker threads. According to Qt's documentation, there are two main ways to create worker threads with QThread : Instantiate QThread directly and create a worker QObject , then call . moveToThread() on the worker using the thread as an argument.

How do you use QThread?

To use it, prepare a QObject subclass with all your desired functionality in it. Then create a new QThread instance, push the QObject onto it using moveToThread(QThread*) of the QObject instance and call start() on the QThread instance. That's all.

How do you stop QThread in Python?

QThread will notify you via a signal when the thread is started() and finished() , or you can use isFinished() and isRunning() to query the state of the thread. You can stop the thread by calling exit() or quit() .

Is PyQt thread safe?

While some parts of the Qt framework are thread safe, much of it is not. The Qt C++ documentation provides a good overview of which classes are reentrant (can be used to instantiate objects in multiple threads).


1 Answers

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
like image 69
Eli Bendersky Avatar answered Oct 09 '22 20:10

Eli Bendersky