From aeb436acfa695670e55629cac8d02e91111fc1ba Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Wed, 2 Dec 2026 13:50:55 +0800 Subject: [PATCH] fix: prevent dpr override for FileUrl icons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Add refreshDtkDevicePixelRatio() wrapper in DQuickIconImagePrivate that returns false early when iconType is FileUrl 2. Update updateDevicePixelRatio() override to delegate to QQuickImagePrivate::updateDevicePixelRatio() for FileUrl icons instead of forcing DTK pixel ratio logic 3. Replace direct calls to updateDevicePixelRatio(1.0) with refreshDtkDevicePixelRatio() in DQuickIconImage::init(), componentComplete(), itemChange(), pixmapChange(), and DQuickDciIconImageItemPrivate::updatePlayer() 4. Update unit test includes to reference the private header and add a new test verifying FileUrl icons preserve the decoded DPR and delegate to Qt Quick's native @Nx lookup The DTK pixel ratio logic was incorrectly overriding Qt Quick's native device pixel ratio handling for raster file icons, which broke the ability of Qt Quick to perform @Nx file lookup. FileUrl icons now bypass the DTK override and use the base QQuickImagePrivate implementation, while theme/svg icon paths retain the previous behavior. Influence: 1. Verify raster image icons (FileUrl) load correct @Nx variants on high-DPI displays 2. Confirm theme name and SVG icons still honor the DTK device pixel ratio calculation 3. Test window change and device pixel ratio change events update icon rendering correctly for both FileUrl and theme icons 4. Run the new ut_DQuickIconImage.fileUrlIgnoresWindowDprRefresh unit test 5. Validate DciIcon images under treeland still refresh their device pixel ratio on player update fix: 修复 FileUrl 图标设备像素比被错误覆盖的问题 1. 在 DQuickIconImagePrivate 中新增 refreshDtkDevicePixelRatio() 包装函 数,当 iconType 为 FileUrl 时提前返回 false 2. 修改 updateDevicePixelRatio() 重写实现,对 FileUrl 图标改为调用基类 QQuickImagePrivate::updateDevicePixelRatio(),不再强制执行 DTK 像素比 逻辑 3. 在 DQuickIconImage::init()、componentComplete()、itemChange()、 pixmapChange() 以及 DQuickDciIconImageItemPrivate::updatePlayer() 中,将 直接调用 updateDevicePixelRatio(1.0) 替换为 refreshDtkDevicePixelRatio() 4. 更新单元测试的私有头文件引用,新增测试用例验证 FileUrl 图标保持解码后 的 DPR 并能委托给 Qt Quick 原生 @Nx 查找 此前 DTK 的像素比逻辑会错误地覆盖 Qt Quick 对栅格文件图标的原生设备像素 比处理,导致 Qt Quick 无法执行 @Nx 文件查找。现在 FileUrl 图标会绕过 DTK 覆盖逻辑并使用基类 QQuickImagePrivate 实现,而主题/SVG 图标路径保持原有 行为。 Influence: 1. 验证栅格图像图标(FileUrl)在高 DPI 屏幕上能正确加载 @Nx 变体 2. 确认主题名称和 SVG 图标仍遵循 DTK 设备像素比计算 3. 测试窗口变化和设备像素比变化事件对 FileUrl 及主题图标的渲染更新是否 正确 4. 运行新增的 ut_DQuickIconImage.fileUrlIgnoresWindowDprRefresh 单元测试 5. 验证 treeland 下 DciIcon 图像在播放器更新时仍会刷新其设备像素比 Log: Fixed device pixel ratio being incorrectly overridden for FileUrl icons PMS: BUG-381363 --- src/private/dquickdciiconimage.cpp | 2 +- src/private/dquickiconimage.cpp | 19 +++++++++++++++---- src/private/dquickiconimage_p_p.h | 3 ++- tests/ut_dquickiconimage.cpp | 24 ++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/private/dquickdciiconimage.cpp b/src/private/dquickdciiconimage.cpp index 53f131a19..f8d7f684f 100644 --- a/src/private/dquickdciiconimage.cpp +++ b/src/private/dquickdciiconimage.cpp @@ -181,7 +181,7 @@ void DQuickDciIconImageItemPrivate::updatePlayer() player->setPalette(palette); // treeland下拿qApp的缩放不准确,初始化的时候尝试去更新一次缩放 - updateDevicePixelRatio(1.0); + refreshDtkDevicePixelRatio(); player->setDevicePixelRatio(devicePixelRatio); } diff --git a/src/private/dquickiconimage.cpp b/src/private/dquickiconimage.cpp index a49fc5556..1128a2751 100644 --- a/src/private/dquickiconimage.cpp +++ b/src/private/dquickiconimage.cpp @@ -14,8 +14,19 @@ DGUI_USE_NAMESPACE DQUICK_BEGIN_NAMESPACE +bool DQuickIconImagePrivate::refreshDtkDevicePixelRatio() +{ + if (iconType == FileUrl) + return false; + + return updateDevicePixelRatio(1.0); +} + bool DQuickIconImagePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio) { + if (iconType == FileUrl) + return QQuickImagePrivate::updateDevicePixelRatio(targetDevicePixelRatio); + #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) if (!qApp->testAttribute(Qt::AA_UseHighDpiPixmaps)) { devicePixelRatio = 1.0; @@ -67,7 +78,7 @@ void DQuickIconImagePrivate::init() D_Q(DQuickIconImage); // 强制确保图标的缩放比例正确 - updateDevicePixelRatio(1.0); + refreshDtkDevicePixelRatio(); if (iconType == ThemeIconName) { QObject::connect(DGuiApplicationHelper::instance()->applicationTheme(), SIGNAL(iconThemeNameChanged(QByteArray)), @@ -173,7 +184,7 @@ void DQuickIconImage::componentComplete() connect(this, &QQuickItem::windowChanged, this, [this](){ D_D(DQuickIconImage); - if (d->updateDevicePixelRatio(1.0)) { + if (d->refreshDtkDevicePixelRatio()) { d->maybeUpdateUrl(); } }); @@ -356,7 +367,7 @@ void DQuickIconImage::itemChange(ItemChange change, const ItemChangeData &value) switch (change) { case ItemDevicePixelRatioHasChanged: { - if (d->updateDevicePixelRatio(1.0)) { + if (d->refreshDtkDevicePixelRatio()) { d->maybeUpdateUrl(); } } @@ -384,7 +395,7 @@ void DQuickIconImage::pixmapChange() // QQuickImage中只会在设置了souceSize的前提下才会计算图片自身的缩放比例 // 此处强制确保图标的缩放比例正确 D_D(DQuickIconImage); - if (d->updateDevicePixelRatio(1.0)) { + if (d->refreshDtkDevicePixelRatio()) { d->maybeUpdateUrl(); } diff --git a/src/private/dquickiconimage_p_p.h b/src/private/dquickiconimage_p_p.h index 847775af6..86d263921 100644 --- a/src/private/dquickiconimage_p_p.h +++ b/src/private/dquickiconimage_p_p.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2020 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later @@ -28,6 +28,7 @@ class DQuickIconImagePrivate : public QQuickImagePrivate DQuickIconImage::Mode getIconMode() const; qreal calculateDevicePixelRatio() const; + bool refreshDtkDevicePixelRatio(); bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override; void updateBase64Image(); diff --git a/tests/ut_dquickiconimage.cpp b/tests/ut_dquickiconimage.cpp index 80526fd44..c975db97c 100644 --- a/tests/ut_dquickiconimage.cpp +++ b/tests/ut_dquickiconimage.cpp @@ -1,11 +1,11 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later #include #include "test_helper.hpp" -#include "dquickiconimage_p.h" +#include "dquickiconimage_p_p.h" #include #include @@ -59,3 +59,23 @@ TEST(ut_DQuickIconImage, image) auto windowImange = helper.object->window()->grabWindow(); EXPECT_EQ(windowImange.pixelColor(QPoint(50, 50)), Qt::red); } + +TEST(ut_DQuickIconImage, fileUrlIgnoresWindowDprRefresh) +{ + DQuickIconImage icon; + icon.setName(QStringLiteral("qrc:/dpr-test/from-name.png")); + + const qreal decodedDpr = 2.0; + icon.d_func()->devicePixelRatio = decodedDpr; + + EXPECT_FALSE(icon.d_func()->refreshDtkDevicePixelRatio()); + EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, decodedDpr); + + // A raster file lets Qt Quick continue to its @Nx lookup. + EXPECT_FALSE(icon.d_func()->updateDevicePixelRatio(1.25)); + EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, decodedDpr); + + icon.setName(QStringLiteral("qrc:/icon/red.svg")); + EXPECT_TRUE(icon.d_func()->updateDevicePixelRatio(1.0)); + EXPECT_DOUBLE_EQ(icon.d_func()->devicePixelRatio, 1.0); +}