Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-70764

drawText ignores current scaling transform

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.11.0
    • GUI: Painting
    • None
    • Ubuntu 14.04
    • Linux/X11

    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;
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vgt Eirik Aavitsland
            10110111 Ruslan Kabatsayev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes