From 1e1c40e1a996b03641fe27b24474fc971b8cfa80 Mon Sep 17 00:00:00 2001 From: Mengci Cai Date: Fri, 6 Feb 2026 13:18:04 +0800 Subject: [PATCH] fix: support scaling for local file icons 1. Add updateFileUrlImage method to handle FileUrl type icons 2. Load local file image and apply manual scaling based on devicePixelRatio 3. Use Qt::SmoothTransformation for quality scaling 4. Modify setName to delegate FileUrl loading to maybeUpdateUrl logic 5. This ensures local file icons scale correctly for high DPI displays Influence: 1. Test loading icons from local file paths on standard and high DPI screens 2. Verify image rendering quality and sharpness after scaling 3. Test behavior when sourceSize is explicitly specified vs default 4. Verify that Base64 and Theme icons still render correctly 5. Check for performance impact with large local image files PMS: BUG-333731 --- src/private/dquickiconimage.cpp | 28 ++++++++++++++++++++++++++-- src/private/dquickiconimage_p_p.h | 3 ++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/private/dquickiconimage.cpp b/src/private/dquickiconimage.cpp index b9d5a3dd3..e7c2945b7 100644 --- a/src/private/dquickiconimage.cpp +++ b/src/private/dquickiconimage.cpp @@ -40,6 +40,28 @@ void DQuickIconImagePrivate::updateBase64Image() setImage(image); } +void DQuickIconImagePrivate::updateFileUrlImage() +{ + Q_ASSERT(iconType == FileUrl); + + D_Q(DQuickIconImage); + + // 处理本地PNG文件 + QUrl url(name); + if (url.isValid()) { + QImage image(url.toLocalFile()); + if (!image.isNull()) { + QSize iconSize = q->sourceSize(); + if (iconSize.isEmpty()) { + iconSize = image.size(); + } + // 应用devicePixelRatio进行缩放 + image = image.scaled(iconSize * devicePixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation); + setImage(image); + } + } +} + QImage DQuickIconImagePrivate::requestImageFromBase64(const QString &name, const QSize &requestedSize, qreal devicePixelRatio) { const QString flag("base64,"); @@ -94,6 +116,8 @@ void DQuickIconImagePrivate::maybeUpdateUrl() if (iconType != ThemeIconName) { if (iconType == Base64Data) updateBase64Image(); + else if (iconType == FileUrl) + updateFileUrlImage(); return; } @@ -216,10 +240,10 @@ void DQuickIconImage::setName(const QString &name) } else if (QQmlFile::isLocalFile(name)) { QUrl url(name); - // 如果name指定的是一个url,则直接将其当作url使用 + // 如果name指定的是一个url,则将其标记为FileUrl类型 + // 不直接设置source,而是通过maybeUpdateUrl统一处理以支持缩放 if (url.isValid()) { d->iconType = DQuickIconImagePrivate::FileUrl; - setSource(url); } } diff --git a/src/private/dquickiconimage_p_p.h b/src/private/dquickiconimage_p_p.h index 847775af6..d18bb9bee 100644 --- a/src/private/dquickiconimage_p_p.h +++ b/src/private/dquickiconimage_p_p.h @@ -31,6 +31,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override; void updateBase64Image(); + void updateFileUrlImage(); static QImage requestImageFromBase64(const QString &name, const QSize &requestedSize, qreal devicePixelRatio); @@ -45,7 +46,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate enum IconType : qint8 { ThemeIconName, // 图标名称 Base64Data, // base64编码的图标图片数据 - FileUrl // 图标文件的url地址 + FileUrl, // 图标文件的url地址 }; // 记录此图标是否应该从图标主题中获取。