PDA

View Full Version : Stylesheet and QFontMetrics



ucomesdag
18th June 2007, 16:07
Is there a way to get QFontMetrics (http://doc.trolltech.com/latest/qfontmetrics.html) from for example a QLabel (http://doc.trolltech.com/latest/qlabel.html) where the font was set with a stylesheet?

wysota
18th June 2007, 16:54
What did you already try to achieve the goal?

ucomesdag
19th June 2007, 00:03
What did you already try to achieve the goal?



myWidget::setMainText(QString mainText, Qt::Alignment alignment)
{

while(this->mainLabel->geometry().width() < QFontMetrics(this->mainLabel->font()).width(mainText))
{
/* Change the font size to make it fit... */
};

this->mainLabel->setText(mainText);
this->mainLabel->setAlignment(alignment);
};With a stylesheet I have set the QLabel mainlabel to Arial but QFontMetrics sees "MS Shell Dlg 2"...

wysota
19th June 2007, 00:54
I think the text doesn't depend on the font size, thus you should adjust the font size while drawing the text and not when setting it on the widget. And probably when you do, you'll have the proper font set up on the painter or at least in the style option initialized from the widget. Text size doesn't make sense outside painting routines. The label won't even have the width calculated before it is first shown and I guess you'd want to have a possibility to call this method before the widget is shown.

ucomesdag
19th June 2007, 04:43
I think the text doesn't depend on the font size, thus you should adjust the font size while drawing the text and not when setting it on the widget. And probably when you do, you'll have the proper font set up on the painter or at least in the style option initialized from the widget. Text size doesn't make sense outside painting routines. The label won't even have the width calculated before it is first shown and I guess you'd want to have a possibility to call this method before the widget is shown.

The QLabels are sized and drawn on screen and the text is set afterwards. So I know it's width and should be able to resize the text when I know the font size. What I understand from what you say is that as long no text is drawn the label will not have the font set? I think it's strange cause it has also a border and background that is set correctly...

wysota
19th June 2007, 08:47
What I understand from what you say is that as long no text is drawn the label will not have the font set?
Let me rephrase - it is possible that font metrics point to the proper font only from within paintEvent and its QStyleOption object. But it's a wild guess, so you should verify that.


think it's strange cause it has also a border and background that is set correctly...
How do you check that?

ucomesdag
19th June 2007, 12:19
How do you check that?

Oups yeah you're right, don't no why I assumed it (because it's drawn on the screen but is also the same case for the text).

When doing the same thing in the paintEvent seems to do the trick, i'm getting the font and it's size correctly..

Thanks Wysota!

h123
18th January 2010, 07:45
Hi,

I am facing similar problem, pl. see http://www.qtcentre.org/threads/27314-Problems-with-StyleSheet-and-QFontMetrics-(see-attached-program)

In above toy program, I am setting font size to 20 as stylesheet to QApplication; but when taking QFontMetrics of mainwindow or dialog it gives me size '9'.

In my actual application, I am allowing user to change the font size of the entire application via style sheet; and for sime tableviews / QComboBoxes / QToolbars I am using QFontMetrics (for which I am getting wrong QFontMetrics information).
Please suggest if there is any alternate way to 'allow user to set font of entire application'.

Thank you in advance.
-Hiral