Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Bounding Text Being Rendered Using QPainter?



  • I'm rendering some text using a QPainter, using the following call:

    @painter->drawText(x, y, Bridge::to_qt(geometry.Data.Text.UTF8String));@

    Prior to this, I'm setting the font for the QPainter. How would I find the bounding box of the text plotted at (x y) using a given QFont? Seems like there should be a way...



  • Hi,

    you can use "this":http://doc.qt.nokia.com/4.7/qpainter.html#drawText-13 drawText oberload to get the bounding rectangle.



  • [quote author="LiamMaru" date="1301932840"]I'm rendering some text using a QPainter, using the following call:

    @painter->drawText(x, y, Bridge::to_qt(geometry.Data.Text.UTF8String));@

    Prior to this, I'm setting the font for the QPainter. How would I find the bounding box of the text plotted at (x y) using a given QFont? Seems like there should be a way...[/quote]

    Since that call doesn't break the text in lines, doesn't handle alignment, etc., you may use QFontMetrics to find out the bounding rectangle of your text.



  • But his drawText also does not use line breaking :-)
    But you are right, QFontMetrics is anothe sollution, perhaps the better one.



  • Actually I was replying to OP, edited the post to make that clear.


Log in to reply