首页 > 解决方案 > 将 Qt 代码从 createTextureFromId 更新为 createTextureFromNativeObject

问题描述

我有一段代码如下所示:

// Before the scene graph starts to render, we update to the pending texture
    void prepareNode()
    {
        m_Lock.lock();
        int newId = m_ID;
        QSize size = m_Size;
        m_ID = 0;
        m_Lock.unlock();

        if (newId)
        {
            delete m_Texture;
            // note: include QQuickWindow::TextureHasAlphaChannel if the rendered content
            // has alpha.
            m_Texture = m_Window->createTextureFromId(newId, size);
            setTexture(m_Texture);

            markDirty(DirtyMaterial);

            // This will notify the rendering thread that the texture is now being rendered
            // and it can start rendering to the other one.
            Q_EMIT textureInUse();
        }
    }

现在将 Qt 更新到 5.15,函数 createTextureFromId 不再受支持,应替换为 createTextureFromNativeObject。

但是,我无法在 Qt 方面找到有关如何从纹理 ID 获取 QTNativeObject 的任何有用文档。(我对Qt的理解低于基础)

注意:更改布局和父函数是一个很大的开销。该函数被称为

connect(m_QMLWindow->m_RenderThread, &QMLRenderThread::textureReady, node, &TextureNode::newTexture,
                Qt::DirectConnection);

我想应该有一种简单的方法来遍历createTextureFromNativeObject?

标签: c++qt

解决方案


你可以试试:

QQuickWindow::CreateTextureOptions texOpts;
if (hasAlphaChannel)
    texOpts.setFlag(QQuickWindow::TextureHasAlphaChannel);
    
m_Texture = m_Window->createTextureFromNativeObject(QQuickWindow::NativeObjectTexture, &newId, 0, size, texOpts);

推荐阅读