PDA

View Full Version : Problems with StyleSheet and QFontMetrics (see attached program)



h123
18th January 2010, 05:51
Hi,

Herewith I have attached a toy program in which it sets the stylesheet "* { font-size: 20pt; }" of app context.

After setting above style sheet, I am taking QFontMetrics to do some processing on fontmetrics.width() / fontmetrics.height() but both are getting wrong.

In toy program if you click "Print Font Info" button, I displays the correct information.
But when showing the fontmetrics of dialog (click on 'Show Dialog') or panel (click on 'Show Panel') it displays wrong.

In short: my aim is to set application font and subsequently I should get the correct fontmetrics info.

Please let me know if I am doing anthing wrong.

Thank you.
-Hiral

h123
18th January 2010, 09:57
It seems that QFontMetrics takes effect only after showing widget.

I moved some of the print statmenets (which prints the QFontMetrics info) after widget has been shown; and observed that it reflects the correct size.

But still, my requirement is not met. I am using QFontMetrics inside the custom Widget's constructors to place/adjust widgets and which gives me incorrect sizes...
Please help.

Thank you.

aamer4yu
18th January 2010, 10:30
Just put the code from the constructor into a function and call that function using QTimer::singleShot().
This way when you use QFontMetrics, the widget will be shown and proper values available.

h123
18th January 2010, 12:49
Hi aamer4yu,

Thank you for your suggesion.

BTW: I did some experiments (with code inside the constructors) and found that if we call the minimumSizeHint() before taking QFontMetrics info then it given correct values !!! Thi is strange !!!
Any idea why is this required ???

Thank you.

Micah
27th June 2021, 02:23
Hi aamer4yu,

Thank you for your suggesion.

BTW: I did some experiments (with code inside the constructors) and found that if we call the minimumSizeHint() before taking QFontMetrics info then it given correct values !!! Thi is strange !!!
Any idea why is this required ???

Thank you.

I realize this is a ten year old question, but I found it while I was googling the same problem :)

minimumSizeHint() calls ensurePolished() internally. You should call that directly, and then grab the QFontMetrics