{"id":5348,"date":"2022-10-21T03:57:05","date_gmt":"2022-10-21T03:57:05","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=5348"},"modified":"2022-11-09T01:53:46","modified_gmt":"2022-11-09T01:53:46","slug":"pyqt-qthread","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qthread\/","title":{"rendered":"PyQt QThread"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PyQt <code>QThread<\/code> to create a responsive Qt application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-pyqt-qthread-class'>Introduction to the PyQt QThread class <a href=\"#introduction-to-the-pyqt-qthread-class\" class=\"anchor\" id=\"introduction-to-the-pyqt-qthread-class\" title=\"Anchor for Introduction to the PyQt QThread class\">#<\/a><\/h2>\n\n\n\n<p>If a program has long-running operations, it may lag for a short moment. In some cases, the program is frozen completely.<\/p>\n\n\n\n<p>Therefore, when developing PyQt programs, you should know how to handle these situations. And to do that, you can take the advantage of threading.<\/p>\n\n\n\n<p class=\"note\">If you&#8217;re not familiar with the <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-threading\/\">threading<\/a> concept, you can learn more about it in the <a href=\"https:\/\/www.pythontutorial.net\/python-concurrency\/\">Python concurrency<\/a> series.<\/p>\n\n\n\n<p>Python has a number of modules for handling threads such as threading and <code>concurrent.futures<\/code>. <\/p>\n\n\n\n<p>While you can use these modules, PyQt provides a better way of doing it by using the <code>QThread<\/code> class and other classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='the-main-thread-and-worker-threads'>The main thread and worker threads <a href=\"#the-main-thread-and-worker-threads\" class=\"anchor\" id=\"the-main-thread-and-worker-threads\" title=\"Anchor for The main thread and worker threads\">#<\/a><\/h3>\n\n\n\n<p>Qt applications are event-based. When you call the <code>exec()<\/code> method, it starts an event loop and creates a thread that is referred to as the <strong>main thread<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/PyQt-Event-Loop.svg\" alt=\"PyQt Event Loop\" class=\"wp-image-4868\"\/><\/figure>\n\n\n\n<p>Any events that occur in the main thread run synchronously within the main event loop. <\/p>\n\n\n\n<p>To take the advantage of threading, you need to create a <strong>secondary thread<\/strong> to offload the long-running operations from the main thread. The secondary threads are often referred to as <strong>worker threads<\/strong>.<\/p>\n\n\n\n<p>To communicate between the main thread and the worker threads, you use signals and slots. The steps for using the <code>QThread<\/code> class are as follows:<\/p>\n\n\n\n<p>First, create a class that inherits from the <code>QObject<\/code> and offloads the long-running operations to this class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Worker<\/span><span class=\"hljs-params\">(QObject)<\/span>:<\/span>\n   <span class=\"hljs-keyword\">pass<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The reason we subclass the <code>QObject<\/code> class is that we want to use the signal and slot.<\/p>\n\n\n\n<p>Next, create a worker thread and worker object from the main thread<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker = Worker()\nself.worker_thread = QThread()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The self is an instance of the <code>QMainWindow<\/code> or <code>QWidget<\/code>.<\/p>\n\n\n\n<p>Then, connect signals and slots of the Worker class with the main thread.<\/p>\n\n\n\n<p>After that, move the worker to the worker thread by calling the <code>moveToThread()<\/code> method of the worker object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker.moveToThread(self.worker_thread)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, start the worker thread:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker_thread.start()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It&#8217;s important to note that you should only communicate with the worker via signals and slots. And you do not call any of its methods from the main thread. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker.do_some_work() <span class=\"hljs-comment\"># DON'T<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that another way of using the <code>QThread<\/code> class is to subclass it and override the <code>run()<\/code> method. However, it is not recommended way. Please find the <a href=\"https:\/\/stackoverflow.com\/questions\/50622536\/movetothread-vs-deriving-from-qthread-in-qt\" target=\"_blank\" rel=\"noreferrer noopener\">detailed answer here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pyqt-qthread-example'>PyQt QThread example <a href=\"#pyqt-qthread-example\" class=\"anchor\" id=\"pyqt-qthread-example\" title=\"Anchor for PyQt QThread example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a simple program that uses the <code>QThread<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"154\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QThread.png\" alt=\"PyQt QThread\" class=\"wp-image-5350\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QThread.png 452w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/10\/PyQt-QThread-300x102.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/figure>\n\n\n\n<p>The program consists of a <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qprogressbar\/\">progress bar<\/a> and a <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qpushbutton\/\">button<\/a>. When you click the start button, the long-running operation will run in a worker thread and update the <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qprogressbar\/\">progress<\/a> back to the main thread via <a href=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-signals-slots\/\">signals and slots<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtWidgets <span class=\"hljs-keyword\">import<\/span> QApplication, QMainWindow, QWidget, QLabel, QPushButton, QVBoxLayout, QProgressBar\n<span class=\"hljs-keyword\">from<\/span> PyQt6.QtCore <span class=\"hljs-keyword\">import<\/span> QThread, QObject, pyqtSignal <span class=\"hljs-keyword\">as<\/span> Signal, pyqtSlot <span class=\"hljs-keyword\">as<\/span> Slot\n<span class=\"hljs-keyword\">import<\/span> time\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Worker<\/span><span class=\"hljs-params\">(QObject)<\/span>:<\/span>\n    progress = Signal(int)\n    completed = Signal(int)\n\n<span class=\"hljs-meta\">    @Slot(int)<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">do_work<\/span><span class=\"hljs-params\">(self, n)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">1<\/span>, n+<span class=\"hljs-number\">1<\/span>):\n            time.sleep(<span class=\"hljs-number\">1<\/span>)\n            self.progress.emit(i)\n\n        self.completed.emit(i)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MainWindow<\/span><span class=\"hljs-params\">(QMainWindow)<\/span>:<\/span>\n    work_requested = Signal(int)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, *args, **kwargs)<\/span>:<\/span>\n        super().__init__(*args, **kwargs)\n\n        self.setGeometry(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">50<\/span>)\n        self.setWindowTitle(<span class=\"hljs-string\">'QThread Demo'<\/span>)\n\n        <span class=\"hljs-comment\"># setup widget<\/span>\n        self.widget = QWidget()\n        layout = QVBoxLayout()\n        self.widget.setLayout(layout)\n        self.setCentralWidget(self.widget)       \n\n        self.progress_bar = QProgressBar(self)\n        self.progress_bar.setValue(<span class=\"hljs-number\">0<\/span>)\n\n        self.btn_start = QPushButton(<span class=\"hljs-string\">'Start'<\/span>, clicked=self.start)\n\n        layout.addWidget(self.progress_bar)\n        layout.addWidget(self.btn_start)\n\n        self.worker = Worker()\n        self.worker_thread = QThread()\n\n        self.worker.progress.connect(self.update_progress)\n        self.worker.completed.connect(self.complete)\n\n        self.work_requested.connect(self.worker.do_work)\n\n        <span class=\"hljs-comment\"># move worker to the worker thread<\/span>\n        self.worker.moveToThread(self.worker_thread)\n\n        <span class=\"hljs-comment\"># start the thread<\/span>\n        self.worker_thread.start()\n\n        <span class=\"hljs-comment\"># show the window<\/span>\n        self.show()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.btn_start.setEnabled(<span class=\"hljs-literal\">False<\/span>)\n        n = <span class=\"hljs-number\">5<\/span>\n        self.progress_bar.setMaximum(n)\n        self.work_requested.emit(n)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">update_progress<\/span><span class=\"hljs-params\">(self, v)<\/span>:<\/span>\n        self.progress_bar.setValue(v)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">complete<\/span><span class=\"hljs-params\">(self, v)<\/span>:<\/span>\n        self.progress_bar.setValue(v)\n        self.btn_start.setEnabled(<span class=\"hljs-literal\">True<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    sys.exit(app.exec())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='defining-the-worker-class'>Defining the worker class <a href=\"#defining-the-worker-class\" class=\"anchor\" id=\"defining-the-worker-class\" title=\"Anchor for Defining the worker class\">#<\/a><\/h3>\n\n\n\n<p>The <code>Worker<\/code> class inherits from <code>QObject<\/code> class so that it can support signals and slots. In practice, you move the long operations to the Worker class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Worker<\/span><span class=\"hljs-params\">(QObject)<\/span>:<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Worker<\/code> class has two signals:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>progress<\/li>\n\n\n\n<li>completed<\/li>\n<\/ul>\n\n\n\n<p>These signals are instances of the <code>pyqtSignal<\/code> class. Because we import the <code><code>pyqtSignal<\/code><\/code> as <code>Signal<\/code>, we can use the <code>Signal<\/code> instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">progress = Signal(int)\ncompleted = Signal(int)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Both progress and completed signal accept an integer.<\/p>\n\n\n\n<p>The <code>Worker<\/code> class will emit the progress signal when a part of the work is done and the completed signal when the work is completed.<\/p>\n\n\n\n<p>The Work class has the <code>do_work()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-meta\">@Slot(int)<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">do_work<\/span><span class=\"hljs-params\">(self, n)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">1<\/span>, n+<span class=\"hljs-number\">1<\/span>):\n        time.sleep(<span class=\"hljs-number\">1<\/span>)\n        self.progress.emit(i)\n\n    self.completed.emit(i)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code><code>do_work()<\/code><\/code> method has the @<code>Slot()<\/code> decorator (or <code>pyqtSlot<\/code>). The @<code>Slot()<\/code> decorator turns the <code>do_work()<\/code> method into a slot.<\/p>\n\n\n\n<p>The @<code>Slot()<\/code> decorator is optional. However, connecting a signal to a decorated Python method can help reduce memory usage and make it slightly faster.<\/p>\n\n\n\n<p>The <code>do_work()<\/code> method accepts an integer. It iterates over a range starting from 1 to the argument. In each iteration, it pauses for one second using the <code>time.sleep()<\/code> and emits the progress signal with the current value using the <code>emit()<\/code> method.<\/p>\n\n\n\n<p>Once finished, the <code>do_work()<\/code> method emits the completed signal with the value last integer value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='communicating-between-the-main-thread-and-the-worker-thread'>Communicating between the main thread and the worker thread <a href=\"#communicating-between-the-main-thread-and-the-worker-thread\" class=\"anchor\" id=\"communicating-between-the-main-thread-and-the-worker-thread\" title=\"Anchor for Communicating between the main thread and the worker thread\">#<\/a><\/h3>\n\n\n\n<p>First, create a signal in the <code>MainWindow<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">work_requested = Signal(int)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a <code>Worker<\/code> object and worker thread:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker = Worker()\nself.worker_thread = QThread()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, connect the progress and completed signal of the worker object with the methods of the main window:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker.progress.connect(self.update_progress)\nself.worker.completed.connect(self.complete)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, connect the <code>work_requested<\/code> signal of the <code>MainWindow<\/code> with the <code>do_work<\/code> method of the worker object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.work_requested.connect(self.worker.do_work)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, move the worker to the worker thread by calling the <code>moveToThread()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker.moveToThread(self.worker_thread)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, start the worker thread:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">self.worker_thread.start()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>QThread<\/code> class to create a worker thread to offload a long operation from the main thread.<\/li>\n\n\n\n<li>Use signals and slots to communicate between the main thread and the worker thread.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5348\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qthread\/\"\n\t\t\t\tdata-post-title=\"PyQt QThread\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5348\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/pyqt\/pyqt-qthread\/\"\n\t\t\t\tdata-post-title=\"PyQt QThread\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PyQt QThread to create a responsive Qt application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4862,"menu_order":34,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5348","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=5348"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5348\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4862"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=5348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}