<?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 pause rendering?]]></title><description><![CDATA[<p dir="auto">I'm trying to prevent rendering while the application state is <code>ApplicationSuspended</code> but am stuck atm with my attempt.</p>
<pre><code>QScopedPointer&lt;QQuickWindow&gt; window ... 

QSemaphore renderSemaphore(1);
bool releaseOnRender {};

QObject::connect(window.data(), &amp;QQuickWindow::beforeRendering,
    qApp, [&amp;] {
        releaseOnRender = renderSemaphore.tryAcquire(1, -1);
    }, Qt::DirectConnection);

QObject::connect(window.data(), &amp;QQuickWindow::afterRendering,
    qApp, [&amp;] {
        if (releaseOnRender) {
            renderSemaphore.release(1);
        }
    }, Qt::DirectConnection);

Qt::ApplicationState prevApplicationState = Qt::ApplicationState::ApplicationActive;

QObject::connect(qApp, &amp;QGuiApplication::applicationStateChanged,
    qApp, [&amp;] (Qt::ApplicationState state) {
        qDebug() &lt;&lt; "Changed state to" &lt;&lt; state;
        if (state != prevApplicationState) {
            if (state == Qt::ApplicationState::ApplicationSuspended) {
                renderSemaphore.acquire(1);
                qDebug() &lt;&lt; "Freezing render";
            } else {
                renderSemaphore.release(1);
                qDebug() &lt;&lt; "Releasing render";
            }
            prevApplicationState = state;
        }
    }, Qt::DirectConnection);
</code></pre>
<p dir="auto">after initially being blocked in <code>renderSemaphore.tryAcquire(1, -1);</code>, <code>QGuiApplication::applicationStateChanged</code> isn't responding either (though it should be a different thread (shouldn't it?)</p>
<p dir="auto">Am happy about any advice / idea on how to block, pause or stop rendering while being <code>ApplicationSuspended</code></p>
]]></description><link>https://forum.qt.io/topic/122554/how-to-pause-rendering</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 13:34:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/122554.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Jan 2021 20:19:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to pause rendering? on Tue, 12 Jan 2021 07:51:37 GMT]]></title><description><![CDATA[<p dir="auto">Something came to mind. You should check that the thread the signal is emitted from isn't the main thread, which'd explain why your code (and mine) deadlocks.</p>
]]></description><link>https://forum.qt.io/post/637422</link><guid isPermaLink="true">https://forum.qt.io/post/637422</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Tue, 12 Jan 2021 07:51:37 GMT</pubDate></item><item><title><![CDATA[Reply to How to pause rendering? on Mon, 11 Jan 2021 03:40:16 GMT]]></title><description><![CDATA[<p dir="auto">I can't tell you why it's not working exactly, but I'd suggest something along these lines:</p>
<pre><code>QAtomicInteger&lt;bool&gt; pauseFlag(false);
QSemaphore pauseSemaphore;

QObject::connect(window.data(), &amp;QQuickWindow::beforeRendering, window.data(), [] () {
    if (pauseFlag.loadAcquire())
        pauseSemaphore.acquire();
});

QObject::connect(qApp, &amp;QGuiApplication::applicationStateChanged, qApp, [] (Qt::ApplicationState state) {
    switch (state)
    {
    case Qt::ApplicationState::ApplicationSuspended:
        pauseFlag.storeRelease(true);
        break;
    case Qt::ApplicationState::ApplicationActive:
        if (pauseFlag.testAndSetRelease(true, false))
            pauseSemaphore.release();
        break;
    }
});
</code></pre>
<p dir="auto">PS. The <code>pauseFlag.storeRelease(true);</code> can probably safely be replaced by a relaxed store.</p>
]]></description><link>https://forum.qt.io/post/637052</link><guid isPermaLink="true">https://forum.qt.io/post/637052</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Mon, 11 Jan 2021 03:40:16 GMT</pubDate></item></channel></rss>