QmlDesigner: Clear material preview cache on detach

The previews are cached based on modelnode internal id, which can
overlap between documents, resulting in briefly showing incorrect
preview when new document is opened. Also, never clearing the cache
leads to leaking memory.

Fixed the issue by clearing the preview cache whenever all materials
are removed from browser (model detach and material library deletion).

Change-Id: Idad39fef62871c4e94f35604e643c7195ae9ec81
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Miikka Heikkinen
2022-11-14 14:37:04 +02:00
parent b10c135a10
commit 889e999f32
3 changed files with 14 additions and 0 deletions

View File

@@ -212,6 +212,7 @@ bool MaterialBrowserView::isTexture(const ModelNode &node) const
void MaterialBrowserView::modelAboutToBeDetached(Model *model)
{
m_widget->materialBrowserModel()->setMaterials({}, m_hasQuick3DImport);
m_widget->clearPreviewCache();
if (m_propertyGroupsLoaded) {
m_propertyGroupsLoaded = false;
@@ -301,6 +302,7 @@ void MaterialBrowserView::nodeAboutToBeRemoved(const ModelNode &removedNode)
// removing the material editor node
if (removedNode.id() == Constants::MATERIAL_LIB_ID) {
m_widget->materialBrowserModel()->setMaterials({}, m_hasQuick3DImport);
m_widget->clearPreviewCache();
return;
}

View File

@@ -62,6 +62,11 @@ public:
m_pixmaps.insert(node.internalId(), pixmap);
}
void clearPixmapCache()
{
m_pixmaps.clear();
}
QPixmap requestPixmap(const QString &id,
QSize *size,
[[maybe_unused]] const QSize &requestedSize) override
@@ -257,6 +262,11 @@ QQuickWidget *MaterialBrowserWidget::quickWidget() const
return m_quickWidget.data();
}
void MaterialBrowserWidget::clearPreviewCache()
{
m_previewImageProvider->clearPixmapCache();
}
QPointer<MaterialBrowserModel> MaterialBrowserWidget::materialBrowserModel() const
{
return m_materialBrowserModel;

View File

@@ -54,6 +54,8 @@ public:
QQuickWidget *quickWidget() const;
void clearPreviewCache();
protected:
bool eventFilter(QObject *obj, QEvent *event) override;