首页 > 解决方案 > 基于 Qt::ApplicationState 暂停 Qt 场景图渲染

问题描述

我正在尝试在 android 上的 Qt 应用程序中暂停渲染。初始状态更改后,应用程序被卡住,因为没有更新到达连接。

QScopedPointer<QQuickWindow> window ... 

QSemaphore renderSemaphore(1);
bool releaseOnRender {};

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

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

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

QObject::connect(qApp, &QGuiApplication::applicationStateChanged,
    qApp, [&] (Qt::ApplicationState state) {
        qDebug() << "Changed state to" << state;
        if (state != prevApplicationState) {
            if (state == Qt::ApplicationState::ApplicationSuspended) {
                renderSemaphore.acquire(1);
                qDebug() << "Freezing render";
            } else {
                renderSemaphore.release(1);
                qDebug() << "Releasing render";
            }
            prevApplicationState = state;
        }
    }, Qt::DirectConnection);

我的实施源于对相关问题的建议

有没有机会实现这一目标?也许使用不同的方法?

标签: c++qtqtquick2

解决方案


推荐阅读