<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to Use Multithreading]]></title><description><![CDATA[<p dir="auto">Hello Guys, I am currently making a interactive GUI for user where I have to display live video feed from camera and some serial data coming from Arduino. Currently the live video feed comes from my USB camera and I have to show this along with the data simultaneously. I have seen videos for Q Thread and Video Display using push Button.</p>
<p dir="auto">My question is how can I achieve display the data and no delay video feed at the same time using multi-threading after pressing just a single button named 'VD Display Button'.</p>
<p dir="auto">I am using pyqt5</p>
]]></description><link>https://forum.qt.io/topic/154775/how-to-use-multithreading</link><generator>RSS for Node</generator><lastBuildDate>Sat, 13 Jun 2026 18:47:22 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/154775.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 Feb 2024 05:54:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 21 Feb 2024 07:44:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> let me chatgpt that for you:</p>
<p dir="auto">To continuously fetch data and update the display in real-time, you can use a separate thread to fetch the data and emit a signal every time new data is available. The main thread can then connect a slot to this signal to update the UI. Here’s a simplified example:</p>
<pre><code>class DataThread(QThread):
    data_signal = pyqtSignal(object)

    def run(self):
        while True:
            data = self.fetch_data()  # replace with your data fetching logic
            self.data_signal.emit(data)
            time.sleep(1)  # sleep for a while to prevent high CPU usage

class ATE_UI(QDialog):
    def __init__(self):
        super(ATE_UI,self).__init__()
        loadUi('ATE_Update.ui',self)
        
        self.Reports_Table.setColumnWidth(0,250)
        
        self.label_4.setText('Flight Mode: VTOL')
        
        timer=QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1)
        self.Test_Button.clicked.connect(self.start_fetching_data)
        self.Save_Button.clicked.connect(self.save_data)

        self.data_thread = DataThread()
        self.data_thread.data_signal.connect(self.update_display)

    def start_fetching_data(self):
        self.data_thread.start()

    def update_display(self, data):
        # update your display with the new data here
        pass

    # ... rest of your code ...

</code></pre>
<p dir="auto">In this example, pressing the Test_Button will start the DataThread, which fetches data in a loop until stopped. Every time new data is fetched, it emits a signal with the data, which is connected to the update_display slot in the main thread. This slot can then update the UI with the new data.</p>
<p dir="auto">Please replace self.fetch_data() with your actual data fetching logic and implement the update_display method to update your UI with the new data. Also, don’t forget to handle stopping the thread when necessary. You might want to add a stop button and connect it to a method that calls self.data_thread.terminate(). Be aware that terminating threads can be dangerous if not handled properly, so it’s better to have a mechanism in your thread to stop it gracefully. For example, you can use a QMutex and a flag to check whether the thread should stop or continue fetching data. If the flag is set, the thread can finish its current iteration and then stop itself by returning from the run method. This way, you can avoid abruptly stopping the thread and potentially leaving resources in an inconsistent state.</p>
<p dir="auto">Please note that this is a simplified example and might not cover all edge cases. Always make sure to handle exceptions and edge cases in your code to prevent crashes and ensure a smooth user experience. Also, keep in mind that updating the UI from a different thread is not safe, and you should always use signals and slots for communication between threads in Qt.</p>
]]></description><link>https://forum.qt.io/post/790832</link><guid isPermaLink="true">https://forum.qt.io/post/790832</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Wed, 21 Feb 2024 07:44:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 28 Feb 2024 05:35:33 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk<br />
So the current <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow ugc">main.py</a> file is:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/ef125fee-3d83-4bc1-8813-e9f92d949418.png" alt="sample.png" class=" img-fluid img-markdown" /><br />
Now I want to know where should I implement this part:<br />
def run_long_task(self):<br />
mutex = QMutex()<br />
worker = Worker(mutex)</p>
<pre><code>    def stop_worker():
        with mutex:
            worker.stop_flag = True

    # Connect the worker's finished signal to stop the worker
    worker.finished.connect(stop_worker)
</code></pre>
<p dir="auto">because I tried but it was not working. It prompt to force close the UI.<br />
@J-Hilk</p>
]]></description><link>https://forum.qt.io/post/791518</link><guid isPermaLink="true">https://forum.qt.io/post/791518</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Wed, 28 Feb 2024 05:35:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Tue, 27 Feb 2024 09:05:25 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk here you go:</p>
<pre><code>import sys
import time
from PyQt5.QtCore import Qt, QThread, QTimer, QObject, QMutex, pyqtSignal
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget

class Worker(QObject):
    finished = pyqtSignal()

    def __init__(self, mutex):
        super().__init__()
        self.mutex = mutex
        self.stop_flag = False

    def run_long_task(self):
        while not self.stop_flag:
            # Simulate some work
            time.sleep(1)
            print("Worker thread: Doing some work...")

        self.finished.emit()

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.clicksCount = 0
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("Thread Example")
        self.resize(300, 150)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)

        # Create widgets
        self.clicksLabel = QLabel("Counting: 0 clicks", self)
        self.clicksLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.countBtn = QPushButton("Click me!", self)
        self.countBtn.clicked.connect(self.count_clicks)
        self.longRunningBtn = QPushButton("Long-Running Task!", self)
        self.longRunningBtn.clicked.connect(self.run_long_task)

        # Set layout
        layout = QVBoxLayout()
        layout.addWidget(self.clicksLabel)
        layout.addWidget(self.countBtn)
        layout.addStretch()
        layout.addWidget(self.longRunningBtn)
        self.centralWidget.setLayout(layout)

    def count_clicks(self):
        self.clicksCount += 1
        self.clicksLabel.setText(f"Counting: {self.clicksCount} clicks")

    def run_long_task(self):
        mutex = QMutex()
        worker = Worker(mutex)

        def stop_worker():
            with mutex:
                worker.stop_flag = True

        # Connect the worker's finished signal to stop the worker
        worker.finished.connect(stop_worker)

        # Start the worker thread
        thread = QThread()
        worker.moveToThread(thread)
        thread.started.connect(worker.run_long_task)
        thread.start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

</code></pre>
]]></description><link>https://forum.qt.io/post/791515</link><guid isPermaLink="true">https://forum.qt.io/post/791515</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Tue, 27 Feb 2024 09:05:25 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Tue, 27 Feb 2024 09:03:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> I'm not an expert in Python, but I assume there are equivalents to Atomics and or mutex? You need to check that variable every so often inside the loop and exit the loop when the condition is set.</p>
<p dir="auto">Let me ask the AI to whip up an example.</p>
]]></description><link>https://forum.qt.io/post/791514</link><guid isPermaLink="true">https://forum.qt.io/post/791514</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Tue, 27 Feb 2024 09:03:08 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Tue, 27 Feb 2024 08:56:01 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk Yes, so in the def run there is a while True running. Will I have to pass the button(s) as an object when I am creating an instance of the DataThread class?</p>
<p dir="auto">Or I need to do something else?</p>
]]></description><link>https://forum.qt.io/post/791510</link><guid isPermaLink="true">https://forum.qt.io/post/791510</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Tue, 27 Feb 2024 08:56:01 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Tue, 27 Feb 2024 08:41:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> usually you call quit() on the thread. Doesn't necessarily work, depends on what you're doing inside the thread. If there's no event loop running, but something like an infinite while loop. You'll have to quit that first.</p>
]]></description><link>https://forum.qt.io/post/791504</link><guid isPermaLink="true">https://forum.qt.io/post/791504</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Tue, 27 Feb 2024 08:41:04 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Tue, 27 Feb 2024 08:24:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a><br />
@J-Hilk<br />
Thank you for that info. I am currently facing an issue with the QThread you suggested.<br />
After I finish with my task I am clicking a button and then going back to login page, but in the background the qthread is still running and printing the output. How should I end that thread when pressed the stop button and again use it when I press the button to start the process.</p>
]]></description><link>https://forum.qt.io/post/791495</link><guid isPermaLink="true">https://forum.qt.io/post/791495</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Tue, 27 Feb 2024 08:24:53 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Thu, 22 Feb 2024 06:14:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> said in <a href="/post/790937">How to Use Multithreading</a>:</p>
<blockquote>
<p dir="auto">Can I have more than 1 worker class inherited from Q Thread?</p>
</blockquote>
<p dir="auto">Sure, each of the worker runs then in its own thread.<br />
"And How can I achieve that?" - in the same way you create one worker.</p>
]]></description><link>https://forum.qt.io/post/790945</link><guid isPermaLink="true">https://forum.qt.io/post/790945</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 22 Feb 2024 06:14:16 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Thu, 22 Feb 2024 04:09:42 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk I have marked it as correct answer. I just wanted to know it for my knowledge that Can I have more than 1 worker class inherited from Q Thread? And How can I achieve that? It can be useful for me in the future.</p>
]]></description><link>https://forum.qt.io/post/790937</link><guid isPermaLink="true">https://forum.qt.io/post/790937</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Thu, 22 Feb 2024 04:09:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 21 Feb 2024 09:35:53 GMT]]></title><description><![CDATA[<p dir="auto">@J-Hilk thank you for the help. I have run a sample code and it is running, I will update here as solved when everything works fine.</p>
]]></description><link>https://forum.qt.io/post/790847</link><guid isPermaLink="true">https://forum.qt.io/post/790847</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Wed, 21 Feb 2024 09:35:53 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 21 Feb 2024 07:44:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> let me chatgpt that for you:</p>
<p dir="auto">To continuously fetch data and update the display in real-time, you can use a separate thread to fetch the data and emit a signal every time new data is available. The main thread can then connect a slot to this signal to update the UI. Here’s a simplified example:</p>
<pre><code>class DataThread(QThread):
    data_signal = pyqtSignal(object)

    def run(self):
        while True:
            data = self.fetch_data()  # replace with your data fetching logic
            self.data_signal.emit(data)
            time.sleep(1)  # sleep for a while to prevent high CPU usage

class ATE_UI(QDialog):
    def __init__(self):
        super(ATE_UI,self).__init__()
        loadUi('ATE_Update.ui',self)
        
        self.Reports_Table.setColumnWidth(0,250)
        
        self.label_4.setText('Flight Mode: VTOL')
        
        timer=QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1)
        self.Test_Button.clicked.connect(self.start_fetching_data)
        self.Save_Button.clicked.connect(self.save_data)

        self.data_thread = DataThread()
        self.data_thread.data_signal.connect(self.update_display)

    def start_fetching_data(self):
        self.data_thread.start()

    def update_display(self, data):
        # update your display with the new data here
        pass

    # ... rest of your code ...

</code></pre>
<p dir="auto">In this example, pressing the Test_Button will start the DataThread, which fetches data in a loop until stopped. Every time new data is fetched, it emits a signal with the data, which is connected to the update_display slot in the main thread. This slot can then update the UI with the new data.</p>
<p dir="auto">Please replace self.fetch_data() with your actual data fetching logic and implement the update_display method to update your UI with the new data. Also, don’t forget to handle stopping the thread when necessary. You might want to add a stop button and connect it to a method that calls self.data_thread.terminate(). Be aware that terminating threads can be dangerous if not handled properly, so it’s better to have a mechanism in your thread to stop it gracefully. For example, you can use a QMutex and a flag to check whether the thread should stop or continue fetching data. If the flag is set, the thread can finish its current iteration and then stop itself by returning from the run method. This way, you can avoid abruptly stopping the thread and potentially leaving resources in an inconsistent state.</p>
<p dir="auto">Please note that this is a simplified example and might not cover all edge cases. Always make sure to handle exceptions and edge cases in your code to prevent crashes and ensure a smooth user experience. Also, keep in mind that updating the UI from a different thread is not safe, and you should always use signals and slots for communication between threads in Qt.</p>
]]></description><link>https://forum.qt.io/post/790832</link><guid isPermaLink="true">https://forum.qt.io/post/790832</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Wed, 21 Feb 2024 07:44:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 21 Feb 2024 07:44:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> said in <a href="/post/790827">How to Use Multithreading</a>:</p>
<blockquote>
<p dir="auto">How can I continuously fetch data and display it at the same time?</p>
</blockquote>
<p dir="auto">I have no idea how you fetch the data - you did not explain what camera and how you're accessing it. And regarding displaying: I already wrote that you can pass the data from the camera fetching thread to the UI thread using signals/slots.</p>
]]></description><link>https://forum.qt.io/post/790831</link><guid isPermaLink="true">https://forum.qt.io/post/790831</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 21 Feb 2024 07:44:05 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Thu, 22 Feb 2024 04:06:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> so what I want to do is I press a button after pressing that button it should go to the method named suppose 'abc'.<br />
In method abc I need to perfrom continuous reading of data until stopped and at the same time update the display real time for the data that is coming.<br />
here is the sample code:<br />
#!/usr/bin/env python3</p>
<p dir="auto">import os, sys, time<br />
from PyQt5 import QtWidgets, QtCore<br />
from PyQt5.QtCore import QLibraryInfo, pyqtSignal, pyqtSlot, QUrl, QTimer, QTime, QDate, QThread<br />
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow<br />
from PyQt5.uic import loadUi<br />
from PyQt5.QtGui import QImage<br />
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent</p>
<p dir="auto">os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(<br />
QLibraryInfo.PluginsPath<br />
)</p>
<p dir="auto">class Sample_UI(QDialog):<br />
def <strong>init</strong>(self):<br />
super(Sample_UI,self).<strong>init</strong>()<br />
loadUi('Sample_Update.ui',self)</p>
<pre><code>    self.Reports_Table.setColumnWidth(0,250)
    
    self.label_4.setText('LOGIC_CONTROL')
    
    timer=QTimer(self)
    timer.timeout.connect(self.showTime)
    timer.start(1)
    self.Test_Button.clicked.connect(self.testing)
    self.Save_Button.clicked.connect(self.save_data)
</code></pre>
<p dir="auto">def logic(self):<br />
import logic_build_library<br />
self.abc=logic_build_library.class1()</p>
<pre><code>    try:
        systat=self.abc.sys_status()
        if systat is not None:
            self.label_12.setStyleSheet('background-color: green; color: black;')
        else:
            self.label_12.setStyleSheet('background-color: red; color: black;')
    except Exception as e:
        self.label_12.setStyleSheet('background-color: red; color: black;')
def showTime(self):
    currentTime = QTime.currentTime()
    today_date=QDate.currentDate()
    displayTxt = today_date.toString()+" "+currentTime.toString('hh:mm:ss')
    self.label_6.setText(displayTxt)
def save_data(self):
    print('Data Saved!')
    login=Login()
    widget.setFixedWidth(360)
    widget.setFixedHeight(360)
    widget.addWidget(login)
    widget.setCurrentIndex(widget.currentIndex()+1)
</code></pre>
<p dir="auto">How can I continuously fetch data and display it at the same time?</p>
]]></description><link>https://forum.qt.io/post/790827</link><guid isPermaLink="true">https://forum.qt.io/post/790827</guid><dc:creator><![CDATA[vidyul]]></dc:creator><pubDate>Thu, 22 Feb 2024 04:06:49 GMT</pubDate></item><item><title><![CDATA[Reply to How to Use Multithreading on Wed, 21 Feb 2024 06:18:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vidyul">@<bdi>vidyul</bdi></a> Not sure what exactly your question is.<br />
If you think you really need more than one thread (do you?) then start a second thread where you retrieve the video frames and forward these frames via signals/slots to UI thread to show them (it is not allowed to manipulate UI from other threads!). For serial communication there is really no need for another threads - it is also asynchronous in Qt like all other stuff.</p>
]]></description><link>https://forum.qt.io/post/790803</link><guid isPermaLink="true">https://forum.qt.io/post/790803</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 21 Feb 2024 06:18:32 GMT</pubDate></item></channel></rss>