PDA

View Full Version : Size in pixels of string in QLabel



Mankua
12th May 2011, 18:32
Hello!

Is it possible to compute the width and height of the text in a QLabel? This size depends on the string contents and the font(s) used in QLabel.

I am adding a dynamic toolbar to my application, and all controls have a QLabel with the name of the tool, and then the control itself... for example:

Brightness: [ 0.0 ] Contrast: [ 0.0 ]

And I need to find the exact size in pixels of the "Brightness:" and "Contrast:" text as shown in screen to know how big my QLabel needs to be. Right now I'm doing this using trial and error.

Any ideas?

Thanks!

mvuori
12th May 2011, 19:19
QFontMetrics will do it: http://doc.trolltech.com/latest/qfontmetrics.html

Example from that page:



QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();

Mankua
12th May 2011, 19:51
Wow... Works great... better than my wildest dream!
Thank you!