Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the sizes of the rendered text on a QPainter?

Tags:

c++

qt

qpainter

I draw in my Qt program on a QPainter the text and various elements round it. I need to get the sizes in pixels which will be occupied by this text.

Can I get somehow the sizes in pixels, knowing a text string and a font?

Thanks.

like image 845
shau-kote Avatar asked Jun 02 '13 01:06

shau-kote


1 Answers

You can use QFontMetrics for the purpose. Following is sample from Qt Docs.

 QFont font("times", 24);
 QFontMetrics fm(font);
 int pixelsWide = fm.width("What's the width of this text?");
 int pixelsHigh = fm.height();
like image 88
Kunal Avatar answered Oct 26 '22 22:10

Kunal