From e2f3608443a9c37cd30c2c6a176a17a067a46c64 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 9 Jan 2024 20:19:12 +0100 Subject: [PATCH] QTextImageHandler: Use QUrl::toLocalFile to convert to local file name Simply stripping away the first six characters does not work on Windows. Amends 2d87c4d881b74619fef966ffb0d7a00cb4ccea50 Pick-to: 6.5 6.2 Fixes: QTBUG-120577 Change-Id: If48ba026785cab784f46109e34ac80e39a990b79 Reviewed-by: Axel Spoerl (cherry picked from commit 52a41bac775bea9057acbc4344a29bb7e63297e3) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 66b732244a3e396dfc5028da82321f10ac73e45b) --- src/gui/text/qtextimagehandler.cpp | 14 ++++++++------ .../qtextimagehandler/tst_qtextimagehandler.cpp | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/gui/text/qtextimagehandler.cpp b/src/gui/text/qtextimagehandler.cpp index 70e89614675..5c56c30711e 100644 --- a/src/gui/text/qtextimagehandler.cpp +++ b/src/gui/text/qtextimagehandler.cpp @@ -23,12 +23,14 @@ static inline QString findAtNxFileOrResource(const QString &baseFileName, { // qt_findAtNxFile expects a file name that can be tested with QFile::exists. // so if the format.name() is a file:/ or qrc:/ URL, then we need to strip away the schema. - QString localFile = baseFileName; - if (localFile.startsWith("file:/"_L1)) - localFile = localFile.sliced(6); - else if (localFile.startsWith("qrc:/"_L1)) - localFile = localFile.sliced(3); - + QString localFile; + const QUrl url(baseFileName); + if (url.isLocalFile()) + localFile = url.toLocalFile(); + else if (baseFileName.startsWith("qrc:/"_L1)) + localFile = baseFileName.sliced(3); + else + localFile = baseFileName; extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, qreal *sourceDevicePixelRatio); return qt_findAtNxFile(localFile, targetDevicePixelRatio, sourceDevicePixelRatio); diff --git a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp index b640efc8e48..855536fff5c 100644 --- a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp +++ b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp @@ -42,7 +42,7 @@ void tst_QTextImageHandler::loadAtNImages_data() QTest::addColumn("imageFile"); QTest::addRow("file") << QFINDTESTDATA("data/image.png"); - QTest::addRow("file_url") << QString("file:/") + QFINDTESTDATA("data/image.png"); + QTest::addRow("file_url") << QUrl::fromLocalFile(QFINDTESTDATA("data/image.png")).toString(); QTest::addRow("resource") << ":/data/image.png"; QTest::addRow("qrc_url") << "qrc:/data/image.png"; }