Core: Hack for HighDPI text drawing on QPixmap with Qt6

A QPainter on a QPixmap behaves differently on Qt 5 and 6 when it comes
to the pixmap's devicePixelRatio. On Qt6/DPR>1, the text in this code
would be rendered outside the pixmap. Forcing the DPR of the pixmap
to 1 while rendering the text fixes the issue.

Since I cannot test on all platforms, nor have a multi monitor setup
with different scalings, I added some cargo cult code which resets the
original DPR after rendering. Just to be on the safe side.

Fixes: QTCREATORBUG-26315
Change-Id: I32ce78a45d52a4c75eb437574b925c85b9295402
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Alessandro Portale
2021-09-24 16:00:22 +02:00
parent f2f1fc8601
commit 44fb173a6e

View File

@@ -437,6 +437,8 @@ static QIcon iconWithText(const QIcon &icon, const QString &text)
QIcon iconWithText; QIcon iconWithText;
for (const QSize &pixmapSize : icon.availableSizes()) { for (const QSize &pixmapSize : icon.availableSizes()) {
QPixmap pixmap = icon.pixmap(pixmapSize); QPixmap pixmap = icon.pixmap(pixmapSize);
const qreal originalPixmapDpr = pixmap.devicePixelRatio();
pixmap.setDevicePixelRatio(1); // Hack for QTCREATORBUG-26315
const int fontSize = pixmap.height() / 4; const int fontSize = pixmap.height() / 4;
const int margin = pixmap.height() / 8; const int margin = pixmap.height() / 8;
QFont font; QFont font;
@@ -447,6 +449,8 @@ static QIcon iconWithText(const QIcon &icon, const QString &text)
QTextOption textOption(Qt::AlignHCenter | Qt::AlignBottom); QTextOption textOption(Qt::AlignHCenter | Qt::AlignBottom);
textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
p.drawText(pixmap.rect().adjusted(margin, margin, -margin, -margin), text, textOption); p.drawText(pixmap.rect().adjusted(margin, margin, -margin, -margin), text, textOption);
p.end();
pixmap.setDevicePixelRatio(originalPixmapDpr);
iconWithText.addPixmap(pixmap); iconWithText.addPixmap(pixmap);
} }
return iconWithText; return iconWithText;