PDA

View Full Version : QFontMetrics not giving proper dimension



Gopala Krishna
6th October 2006, 17:12
Hello,
My problem is that QFontMetrics is not returning proper widths and heights of text in my program. I verified this by printing all text info of word "hello" in sample separate program and main program too.
The sample works properly and it was manually compiled with g++ while the other doesn't.

While in the sample , the info read
Family=Sans Serif, Point Size = 11, Width of hello = 33
the info from my original app read
Family=Sans Serif, Point Size = 11, Width of hello = 24 :confused:

And also, though I have commented *.setFont() , resize() and other geometry functions ,i still get same problem described above.
Because of this, my app looks very ugly with truncations and variations in font sizes in all used Qt widgets :( .

Is there any chance that the ordering of libs being linked is causing trouble ?

I tried all programs described above with Qt 4.1, and 2 snapshots of QT 4.2 on my linux system. (g++ 4.0.2)

Actually i had posted my problem in more vague form before in this thread http://www.qtcentre.org/forum/f-qt-programming-2/t-qgroupbox-title-truncated-on-both-sides-3874.html
Later i really tried hard to figure out the problem and noticed that it was all to do with QFontMetrics.

Please do help me solve this problem.
Thanks in advance. :)

jacek
6th October 2006, 20:12
Do you have any static data in your application (esp. QFont instances)?

Gopala Krishna
7th October 2006, 13:20
Hello Jacek,
Thanks for the hint. Problem detected and solved !! :)
Thank you very much !!


Do you have any static data in your application (esp. QFont instances)?
Yes, there was a qfont instance wrapped in a global struct (but not static explicitly) but the structure was declared before main(). I just commented the QFont entries and then a MAGIC - it worked properly. :)

Can you enlighten me why this happens ? What other classes in qt can cause problems when declared static (or global too) ?

jacek
7th October 2006, 16:00
Can you enlighten me why this happens ?
Qt uses some 3rd party libraries to handle fonts and they might require some initialization and this usually happens when you create Q(Core)?Application instance.


What other classes in qt can cause problems when declared static (or global too) ?
I would suspect these classes and methods:
QCoreApplication::application*Path()
QApplication::desktop()
QApplication::palette()
QFont
QFontDatabase
QLibraryInfo
QWidget
QX11Info
There was also some problem with QObject, but AFAIR it was resolved.

The best way is to avoid global variables and to initialize static variables after the main was invoked.