diff options
author | Bastien Bouclet <bastien.bouclet@gmail.com> | 2021-01-22 11:14:22 +0100 |
---|---|---|
committer | Bastien Bouclet <bastien.bouclet@gmail.com> | 2021-01-29 14:18:42 +0100 |
commit | b61275ee72ae2a895dee3652aca886859962cdb2 (patch) | |
tree | 4a9046a5c894cd5ecfe7570575293d87e3e8ea2d /tests/auto/gui | |
parent | 6e7628821dc4cb3b4f1a6356bbe839e94c58a81f (diff) |
QIcon: Use the @nx pixmaps in the paint method
Make use of the device pixel ratio in the QIcon paint method so the @nx
hi-dpi pixmaps are selected when appropriate when painting to a
QPainter.
Fixes: QTBUG-90042
Change-Id: I53995a2285ef879e3c4fddb9f8da702e256a260f
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
(cherry picked from commit b69b04c4790f319104a1098147cea9405b6e7062)
Reviewed-by: Bastien Bouclet <bastien.bouclet@gmail.com>
Diffstat (limited to 'tests/auto/gui')
-rw-r--r-- | tests/auto/gui/image/qicon/tst_qicon.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/auto/gui/image/qicon/tst_qicon.cpp b/tests/auto/gui/image/qicon/tst_qicon.cpp index ece9941dfb..807770b18f 100644 --- a/tests/auto/gui/image/qicon/tst_qicon.cpp +++ b/tests/auto/gui/image/qicon/tst_qicon.cpp @@ -31,6 +31,7 @@ #include <QImageReader> #include <qicon.h> #include <qiconengine.h> +#include <QPainter> #include <QtCore/QStandardPaths> #include <algorithm> @@ -56,6 +57,7 @@ private slots: void detach(); void addFile(); void pixmap(); + void paint(); void availableSizes(); void name(); void streamAvailableSizes_data(); @@ -461,6 +463,52 @@ void tst_QIcon::pixmap() QVERIFY(icon.pixmap(QSize(16, 16), -1).size().width() >= 16); } +void tst_QIcon::paint() +{ + QImage img16_1x(16, 16, QImage::Format_ARGB32); + img16_1x.fill(qRgb(0, 0, 0xff)); + img16_1x.setDevicePixelRatio(1.); + + QImage img16_2x(32, 32, QImage::Format_ARGB32); + img16_2x.fill(qRgb(0, 0xff, 0xff)); + img16_2x.setDevicePixelRatio(2.); + + QImage img32_1x(32, 32, QImage::Format_ARGB32); + img32_1x.fill(qRgb(0xff, 0, 0)); + img32_1x.setDevicePixelRatio(1.); + + QImage img32_2x(64, 64, QImage::Format_ARGB32); + img32_2x.fill(qRgb(0x0, 0xff, 0)); + img32_2x.setDevicePixelRatio(2.); + + QIcon icon; + icon.addPixmap(QPixmap::fromImage(img16_1x)); + icon.addPixmap(QPixmap::fromImage(img16_2x)); + icon.addPixmap(QPixmap::fromImage(img32_1x)); + icon.addPixmap(QPixmap::fromImage(img32_2x)); + + // Test painting the icon version with a device independent size of 32x32 + QRect iconRect(0, 0, 32, 32); + + auto imageWithPaintedIconAtDpr = [&](qreal dpr) { + QImage paintDevice(64 * dpr, 64 * dpr, QImage::Format_ARGB32); + paintDevice.setDevicePixelRatio(dpr); + + QPainter painter(&paintDevice); + icon.paint(&painter, iconRect); + return paintDevice; + }; + + QImage imageWithIcon1x = imageWithPaintedIconAtDpr(1.0); + QCOMPARE(imageWithIcon1x.pixel(iconRect.center()), qRgb(0xff, 0, 0)); + + QImage imageWithIcon2x = imageWithPaintedIconAtDpr(2.0); + QCOMPARE(imageWithIcon2x.pixel(iconRect.center()), qRgb(0, 0xff, 0)); + + QImage imageWithIcon3x = imageWithPaintedIconAtDpr(3.0); + QCOMPARE(imageWithIcon3x.pixel(iconRect.center()), qRgb(0, 0xff, 0)); +} + static bool sizeLess(const QSize &a, const QSize &b) { return a.width() < b.width(); |