c++qtqfontmetrics

How to get the height from the top of character to its base line (i.e, its actual ascent) in QWidget?


I am experimenting to build a tool that can display modified text (i.e, with some additional stroke) on screen using QWidget. So, to put the stroke in its correct position, I need to know the ascent height of the character the stroke is being put on.

And I'm a tad stuck on retrieving the actual ascent of a character. I have tried some of the following things:

How do I solve this problem?


Solution

  • You can use QFontMetrics::boundingRect. The QRect returned will have its origin at (0, 0) with the ascent for character c represented by...

    -QFontMetrics::boundingRect(c).top()
    

    and, similarly, the descent by...

    QFontMetrics::boundingRect(c).bottom()