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

QFontMetrics::boundingRect() returns too-small result

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Duplicate
    • Affects Version/s: 4.7.0, 5.4.0
    • Fix Version/s: Some future release
    • Component/s: GUI: Font handling
    • Labels:
      None
    • Environment:
      Windows 7

      Description

      QFontMetrics::boundingRect() often returns a rectangle that is too narrow. I can reproduce this same behavior with QFontMetrics::boundingRect() and QFontMetricsF::boundingRect(). Some fonts work, some do not. QFontMetrics::tightBoundingRect() and QFontMetricsF::tightBoundingRect() also seem to fail, although it is more difficult to see because there is no drawText() method that can make good use of a QRect returned from tightBoundingRect(). (At least, not that I could figure out.) It seems to happen most with small fonts, which is why I have the QPainter scale transform, so the problem is easily seen. I first noticed this on a QGraphicsScene using QGraphicsItem elements that draw text.

      main.cpp
      #include <QtGui>
      
      int main(int argc, char **argv) {
        QApplication app(argc, argv);
      
        const QString kText = "Salem";
      
        QFont font("MS Shell Dlg 2");  // Arial, Arial Black also fail; Courier New, Verdana, Times New Roman are fine.
        font.setPixelSize(11);
      
        QFontMetrics metrics(font);  // QFontMetricsF also fails.
        QRect text_bounding_rect = metrics.boundingRect(kText);  // tightBoundingRect() also seems to fail.
        text_bounding_rect.moveTopLeft(QPoint(0, 0));
      
        QPixmap pm(200, 200);
        pm.fill(Qt::white);
      
        QPainter painter(&pm);
        QTransform transform = QTransform::fromScale(3.0, 3.0);
        painter.setTransform(transform);
      
        painter.setBrush(QBrush(Qt::yellow));
        painter.setPen(Qt::NoPen);
        painter.drawRect(text_bounding_rect);
      
        painter.setBrush(Qt::NoBrush);
        painter.setPen(Qt::SolidLine);
        painter.setFont(font);
        painter.drawText(text_bounding_rect, kText);
      
        QLabel lbl;
        lbl.setPixmap(pm);
        lbl.show();
      
        return app.exec();
      }
      

        Attachments

          Issue Links

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

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              dmateer Dave Mateer
              Votes:
              3 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:

                  Gerrit Reviews

                  There are no open Gerrit changes