PDA

View Full Version : Find the text width in QFont



anbu01
17th August 2016, 13:00
I have two items in my tree model i have small difference in text alignment.Is this caused by the width of the text but i checked the width of text using QFontMetrics::width() but both text are same.

Text1:111601756
Text2:999999996

12075

As from the image you can see there is a slight alignment problem in the second text.

Here is the sample code i tried :-


QFont font("times",24);
QFontMetrics metrics(font);
qDebug() << "Width 1" << metrics.width(QString::number(111111111));
qDebug() << "Width 2" << metrics.width(QString::number(999999999));

Output:

Width 1 153

Width 2 153

I expected Width 2 to be more what is going wrong here.Is the method i am checking is wrong.

Radek
17th August 2016, 17:51
Try to print "11111111" and "99999999" using times 24pt and see. Certain proportional fonts use fixed width for numbers so that they can align numeric values. Maybe, times uses the fixed width, too. The font from your picture does not align: "1" is clearly narrower than "9".

anbu01
18th August 2016, 07:15
Thanks it helped i changed the font.