Details
-
Bug
-
Resolution: Duplicate
-
P3: Somewhat important
-
4.7.0, 5.4.0
-
None
-
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.
#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
- duplicates
-
QTBUG-7768 fontMetrics.boundingRect() and drawText(boundingRect) disagree about text width metrics
- Closed
-
QTBUG-70184 QFontMetrics::tightBoundingRect doesn’t return correct width somtimes
- Closed
- relates to
-
QTBUG-85936 TextMetrics.width does not always give a correct width for the given text and is sometimes too short depending on the font used
- Closed