From 44fb173a6e147c35028e46bc4a65d16f791c254d Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Fri, 24 Sep 2021 16:00:22 +0200 Subject: [PATCH] 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 --- src/plugins/coreplugin/iwizardfactory.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/plugins/coreplugin/iwizardfactory.cpp b/src/plugins/coreplugin/iwizardfactory.cpp index d5f02bcb3d0..b70cd1a2d03 100644 --- a/src/plugins/coreplugin/iwizardfactory.cpp +++ b/src/plugins/coreplugin/iwizardfactory.cpp @@ -437,6 +437,8 @@ static QIcon iconWithText(const QIcon &icon, const QString &text) QIcon iconWithText; for (const QSize &pixmapSize : icon.availableSizes()) { 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 margin = pixmap.height() / 8; QFont font; @@ -447,6 +449,8 @@ static QIcon iconWithText(const QIcon &icon, const QString &text) QTextOption textOption(Qt::AlignHCenter | Qt::AlignBottom); textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); p.drawText(pixmap.rect().adjusted(margin, margin, -margin, -margin), text, textOption); + p.end(); + pixmap.setDevicePixelRatio(originalPixmapDpr); iconWithText.addPixmap(pixmap); } return iconWithText;