Details
-
Bug
-
Resolution: Invalid
-
Not Evaluated
-
None
-
5.11.0
-
None
-
Ubuntu 14.04
Description
If I use QPainter::scale and then try QPainter::drawText, text will be rendered in wrong size at wrong place (instead of 10px font size set via QFont::setPixelSize it'll draw in a huge font). One has to reset the transform and calculate the appropriate position manually.
See the following demo (run it with --show-issue argument to see the problem):
#include <QApplication> #include <QImage> #include <QPainter> #include <QDebug> int main(int argc, char** argv) { QApplication app(argc, argv); bool showIssue=false; if(argc>1 && QString(argv[1])=="--show-issue") showIssue=true; const int w=1000, h=1000; QImage img(w,h, QImage::Format_RGBX8888); img.fill(Qt::gray); QPainter p(&img); p.scale(w,-h); p.translate(0,-1); auto font=p.font(); font.setPixelSize(w/20.); p.setFont(font); const char text[]="235"; const QPointF textPos(0.3, 0.3); if(showIssue) { qInfo() << "Rendering result will show the issue"; const auto pos=textPos; p.drawText(QRectF(pos-QPointF(w,h)/2, QSize(w,h)), Qt::AlignCenter, text); } else { qInfo() << "Rendering result will be correct due to workaround"; p.save(); p.resetTransform(); const auto pos=QPointF(textPos.x()*w,h-w*textPos.y()); p.drawText(QRectF(pos-QPointF(w,h)/2, QSize(w,h)), Qt::AlignCenter, text); p.restore(); } const char filename[]="/tmp/test.png"; if(!img.save(filename)) { qCritical() << "Failed to save image to" << filename; return 1; } qInfo() << "Saved to" << filename; }