<?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[Qt multi-threaded display synchronization problem]]></title><description><![CDATA[<p dir="auto">We developed an image processing and display program based on Qt. We use sub-threads for image processing and the main thread as UI display. In order to display synchronization, we use the Qt::BlockingQueuedConnection parameter when connecting the signal-slot. There are other threads that may overwrite the image data, so we use locks. code show as below:</p>
<pre><code>class ProcessThread : public QThread
{
    Q_OBJECT
public:
    ProcessThread(QObject *parent = nullptr);

    void run() override
    {
        int i=0;
        while (1)
        {
            if(isQuit)break;

            mutex.lock();

            QString filename=QString("/home/%1.bmp").arg((i++)%2000);
            data=imread(filename.toStdString());

            //dosomthing...

            emit processCompleted(&amp;data);

            mutex.unlock();
        }
    }

    void setIsQuit(bool v){isQuit=v;}

signals:
    void processCompleted(const Mat *data);

private:
    bool isQuit=false;
    Mat data;
    QMutex mutex;
};

</code></pre>
<pre><code>class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr): QWidget(parent)
    {
        thread=new ProcessThread();

        connect(thread,SIGNAL(processCompleted(const Mat *)),
                this,SLOT(processCompleted(const Mat *)),Qt::BlockingQueuedConnection);

        thread-&gt;start();
    }

    ~Widget()
    {
        thread-&gt;setIsQuit(true);

        thread-&gt;quit();
        thread-&gt;wait();
        delete thread;
    }

    void paintEvent(QPaintEvent *event) override
    {
        Q_UNUSED(event)
        QPainter p(this);
        p.drawImage(QPoint(0,0),image);
    }

private slots:
    void processCompleted(const Mat *data)
    {
        if(data!=nullptr)
        {
            if(data-&gt;channels()==1)
            {
                image=QImage(data-&gt;data,
                             data-&gt;cols,
                             data-&gt;rows,
                             data-&gt;step,
                             QImage::Format_Grayscale8);
            }
            else
            {
                image=QImage(data-&gt;data,
                             data-&gt;cols,
                             data-&gt;rows,
                             data-&gt;step,
                             QImage::Format_RGB888);
            }
        }
        else
        {
            image=QImage();
        }

        repaint();
    }

private:
    QImage image;
    ProcessThread *thread;
};
</code></pre>
<pre><code>int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
</code></pre>
<p dir="auto">1、When I exit the program, it will be stuck in thread-&gt;wait() in the ~Widget() function; it cannot continue to execute;<br />
2、If I comment out thread-&gt;wait(); it will not get stuck when exiting, but it will prompt: "QMutex: destroying locked mutex" and "QThread: Destroyed while thread is still running" errors;<br />
3、、If the Qt::BlockingQueuedConnection parameter is not used when connecting the signal-slot, the first problem above will not occur, but the display will not be synchronized.</p>
<p dir="auto">If BlockingQueuedConnection is not used, the processing thread will not be synchronized with the UI main thread, that is, the processing thread will not wait for the display refresh of the UI main thread, which will cause display confusion. So we need to display synchronization. And I put the image processing thread in the QtConcurrent framework, but it still deadlocks when wairForFinished().</p>
<p dir="auto">How to achieve multi-threaded display synchronization without causing blocking?  Thanks!</p>
]]></description><link>https://forum.qt.io/topic/137074/qt-multi-threaded-display-synchronization-problem</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 16:27:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/137074.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Jun 2022 07:03:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt multi-threaded display synchronization problem on Wed, 08 Jun 2022 17:57:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Beside the memory management issue pointed by <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a>, can you explain what synchronization you are talking about ? Your code has a single worker thread and your GUI shows only the result.</p>
<p dir="auto">You should rather take inspiration from the <a href="https://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">Mandelbrot example</a>.</p>
]]></description><link>https://forum.qt.io/post/716938</link><guid isPermaLink="true">https://forum.qt.io/post/716938</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 08 Jun 2022 17:57:38 GMT</pubDate></item><item><title><![CDATA[Reply to Qt multi-threaded display synchronization problem on Wed, 08 Jun 2022 15:38:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/hujh">@<bdi>hujh</bdi></a> said in <a href="/post/716830">Qt multi-threaded display synchronization problem</a>:</p>
<blockquote>
<p dir="auto">3、、If the Qt::BlockingQueuedConnection parameter is not used when connecting the signal-slot, the first problem above will not occur, but the display will not be synchronized.</p>
</blockquote>
<p dir="auto">Because you pass a pointer to data which you instantly overwrite with new data - what do you expect?</p>
]]></description><link>https://forum.qt.io/post/716927</link><guid isPermaLink="true">https://forum.qt.io/post/716927</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 08 Jun 2022 15:38:24 GMT</pubDate></item></channel></rss>