QIcon: prefer downscaling over upscaling pixmap icons

... instead of just choosing the closest scaling factor.
Otherwise we end up with the 1x pixmap for a 1.25 scaling factor instead
of a the better fitting 2x pixmap.

Task-number: QTBUG-90634
Change-Id: Ic554fc8d2715deea43bc22e71414902a263b2fef
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
David Schulz 2023-04-19 10:14:43 +02:00 committed by Marcus Tillmanns
parent 2b7908ac3a
commit 0d9387a942
2 changed files with 21 additions and 0 deletions

View File

@ -169,6 +169,9 @@ static QPixmapIconEngineEntry *bestSizeScaleMatch(const QSize &size, qreal scale
qreal ascore = pa->scale - scale;
qreal bscore = pb->scale - scale;
// always prefer positive scores to prevent upscaling
if ((ascore < 0) != (bscore < 0))
return bscore < 0 ? pa : pb;
// Take the one closest to 0
return (qAbs(ascore) < qAbs(bscore)) ? pa : pb;
}

View File

@ -17,6 +17,7 @@ private slots:
void addPixmap_data();
void addPixmap();
void ninePatch();
void preferUpscale();
};
tst_QIconHighDpi::tst_QIconHighDpi()
@ -211,6 +212,23 @@ void tst_QIconHighDpi::ninePatch()
}
}
void tst_QIconHighDpi::preferUpscale()
{
QIcon icon;
// manual pixmap adder for full control of devicePixelRatio
auto addPixmapWithDpr = [&icon](const QString &path, qreal dpr) {
QImage image(path);
image.setDevicePixelRatio(dpr);
icon.addPixmap(QPixmap::fromImage(image));
};
addPixmapWithDpr(":/icons/testtheme/22x22/actions/appointment-new.png", 1);
addPixmapWithDpr(":/icons/testtheme/22x22@2/actions/appointment-new.png", 2);
QCOMPARE(icon.pixmap(QSize(22, 22), 1.25f).devicePixelRatio(), 1.25f);
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);