[英]QML: How to delete a global QSGTexture object?
我有QQuickItem派生类(MyItem),它仅绘制纹理(QSGTexture)。 因为所有MyItem都绘制相同的纹理,所以我在它们之间共享了一个QSGTexture实例。 该实例是在首次访问时创建的:
QSGTexture *MyItem::getGlobalTexture()
{
static auto tex = window->createTextureFromImage(QImage{s_textureName});
return tex;
}
一切都很好,但我想以某种方式在销毁应用程序时删除此纹理。
我的第一个想法是为其设置一个父对象,并且我选择了一个QQuickWindow,但是这是不可能的,因为它们位于不同的线程上:
window - mainThread, tex - SGRenderThread
另一种方法是在MyApp析构函数中将其删除,但此调用也将从mainThread中进行,并且SGRenderThread可能已被删除。
还有一个想法是将QCoreApplication::aboutToQuit
信号与QueuedConnection
类型一起使用,因此,如果SGRenderThread
仍然存在,则将在SGRenderThread
上进行删除,并且不再绘制任何帧。
删除全局QSGTexture对象的最佳正确方法是什么?
我已经找到了以下解决方案,实际上是问题中的第三个想法,它似乎适用于线程和非线程场景图
QSGTexture *MyItem::getGlobalTexture(QQuickWindow *window)
{
static QSGTexture *texture = [window]{
auto tex = window->createTextureFromImage(QImage{s_textureName});
//will delete the texture on the GSthread
QObject::connect(qApp, &QCoreApplication::aboutToQuit, tex, [tex]{ tex->deleteLater(); });
return tex;
}();
return texture;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.