Reputation: 12415
I want to draw some text using QPainter::drawText
command.
In order to set its position properly I need to know the dimension of drawn string in pixels.
Is there a way to know pixel dimension of the string? Possibly without drawing it before?
Upvotes: 0
Views: 1276
Reputation: 4873
The QFontMetrics
class has a method just for this purpose: boundingRect()
.
From the Qt docs (http://doc.qt.io/qt-5/qfontmetrics.html#boundingRect-2):
Returns the bounding rectangle of the characters in the string specified by text.
Note that the bounding rect only provides the width of drawn text, the width()
function provides the width of trailing spaces as well. Also from the Qt docs:
boundingRect() returns a rectangle describing the pixels this string will cover whereas width() returns the distance to where the next string should be drawn.
Upvotes: 1