using qt threads in pyside

  • Last Update :
  • Techknowledgy :

Begins execution of the thread by calling PySide.QtCore.QThread.run() , which should be reimplemented in a PySide.QtCore.QThread subclass to contain your code. The operating system will schedule the thread according to the priority parameter. If the thread is already running, this function does nothing.,The starting point for the thread. After calling PySide.QtCore.QThread.start() , the newly created thread calls this function. The default implementation simply calls exec() .,Returns a pointer to a PySide.QtCore.QThread which represents the currently executing thread.,Constructs a new thread with the given parent . The thread does not begin executing until PySide.QtCore.QThread.start() is called.

class MyThread(QThread):
   def run():
   socket = QTcpSocket()
# connect QTcpSocket 's signals somewhere meaningful
#...
   socket.connectToHost(hostName, portNumber)
self.exec_()

Suggestion : 2

The PySide.QtCore.QThread class provides a platform-independent way to manage threads.,Another way to make code run in a separate thread, is to subclass PySide.QtCore.QThread and reimplement PySide.QtCore.QThread.run() . For example:,A PySide.QtCore.QThread object manages one thread of control within the program. QThreads begin executing in PySide.QtCore.QThread.run() . By default, PySide.QtCore.QThread.run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.,Constructs a new PySide.QtCore.QThread to manage a new thread. The parent takes ownership of the PySide.QtCore.QThread . The thread does not begin executing until PySide.QtCore.QThread.start() is called.

<Code snippet "doc/src/snippets/code/src_corelib_thread_qthread.cpp:worker" not found>
<Code snippet "code/src_corelib_thread_qthread.cpp:reimpl-run" not found>

Suggestion : 3

A QThread object manages one thread of control within the program. QThreads begin executing in run() . By default, run() starts the event loop by calling exec() and runs a Qt event loop inside the thread.,Constructs a new QThread to manage a new thread. The parent takes ownership of the QThread . The thread does not begin executing until start() is called.,In that example, the thread will exit after the run function has returned. There will not be any event loop running in the thread unless you call exec() .,Begins execution of the thread by calling run() . The operating system will schedule the thread according to the priority parameter. If the thread is already running, this function does nothing.

class Worker(QObject):

   Q_OBJECT
slots: = public()
def doWork(parameter):
   result = QString()
/* ... here is the expensive or blocking operation ... */
resultReady.emit(result)

signals:
   def resultReady(result):

   class Controller(QObject):

   Q_OBJECT
workerThread = QThread()
# public
Controller() {
      worker = Worker()
      worker.moveToThread(workerThread)
      connect(workerThread, QThread.finished, worker, QObject.deleteLater)
      connect(self, Controller::operate, worker, Worker::doWork)
      connect(worker, Worker::resultReady, self, Controller::handleResults)
      workerThread.start()

         ~Controller() {
            workerThread.quit()
            workerThread.wait()

            slots: = public()
            def handleResults():
               signals:
               def operate():
class WorkerThread(QThread):

   Q_OBJECT
def run():
   result = QString()
/* ... here is the expensive or blocking operation ... */
resultReady.emit(result)

signals:
   def resultReady(s):

   def startWorkInAThread(self):

   workerThread = WorkerThread(self)
connect(workerThread, WorkerThread::resultReady, self, MyObject::handleResults)
connect(workerThread, WorkerThread.finished, workerThread, QObject.deleteLater)
workerThread.start()
void long_task() {
   forever {
      if (QThread::currentThread() - > isInterruptionRequested()) {
         return;
      }
   }
}

Suggestion : 4
python
app = QApplication([])
window = MainWindow()
app.exec_()
python
from PySide2.QtWidgets
import QVBoxLayout, QLabel, QPushButton, QWidget, QMainWindow, QApplication
from PySide2.QtCore
import QTimer

import sys
import time

class MainWindow(QMainWindow):

   def __init__(self):
   super(MainWindow, self).__init__()

self.counter = 0

layout = QVBoxLayout()

self.l = QLabel("Start")
b = QPushButton("DANGER!")
b.pressed.connect(self.oh_no)

layout.addWidget(self.l)
layout.addWidget(b)

w = QWidget()
w.setLayout(layout)

self.setCentralWidget(w)

self.show()

self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()

def oh_no(self):
   time.sleep(5)

def recurring_timer(self):
   self.counter += 1
self.l.setText("Counter: %d" % self.counter)

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
bash
python3 multithread.py
python
QApplication.processEvents()
python
def oh_no(self):
   for n in range(5):
   QApplication.processEvents()
time.sleep(1)
python
from PySide2.QtWidgets
import QVBoxLayout, QLabel, QPushButton, QWidget, QMainWindow, QApplication

import sys
import time

class MainWindow(QMainWindow):

   def __init__(self, * args, ** kwargs):
   super(MainWindow, self).__init__( * args, ** kwargs)

self.counter = 0

layout = QVBoxLayout()

self.l = QLabel("Start")
b = QPushButton("DANGER!")
b.pressed.connect(self.oh_no)

c = QPushButton("?")
c.pressed.connect(self.change_message)

layout.addWidget(self.l)
layout.addWidget(b)

layout.addWidget(c)

w = QWidget()
w.setLayout(layout)

self.setCentralWidget(w)

self.show()

def change_message(self):
   self.message = "OH NO"

def oh_no(self):
   self.message = "Pressed"

for n in range(100):
   time.sleep(0.1)
self.l.setText(self.message)
QApplication.processEvents()

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()