Results 1 to 8 of 8

Thread: Stylesheet and QFontMetrics

  1. #1
    ucomesdag Guest

    Default Stylesheet and QFontMetrics

    Is there a way to get QFontMetrics from for example a QLabel where the font was set with a stylesheet?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stylesheet and QFontMetrics

    What did you already try to achieve the goal?

  3. #3
    ucomesdag Guest

    Default Re: Stylesheet and QFontMetrics

    Quote Originally Posted by wysota View Post
    What did you already try to achieve the goal?

    Qt Code:
    1. myWidget::setMainText(QString mainText, Qt::Alignment alignment)
    2. {
    3.  
    4. while(this->mainLabel->geometry().width() < QFontMetrics(this->mainLabel->font()).width(mainText))
    5. {
    6. /* Change the font size to make it fit... */
    7. };
    8.  
    9. this->mainLabel->setText(mainText);
    10. this->mainLabel->setAlignment(alignment);
    11. };
    To copy to clipboard, switch view to plain text mode 
    With a stylesheet I have set the QLabel mainlabel to Arial but QFontMetrics sees "MS Shell Dlg 2"...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stylesheet and QFontMetrics

    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.

  5. The following user says thank you to wysota for this useful post:


  6. #5
    ucomesdag Guest

    Default Re: Stylesheet and QFontMetrics

    Quote Originally Posted by wysota View Post
    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...

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Stylesheet and QFontMetrics

    Quote Originally Posted by ucomesdag View Post
    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?

  8. The following user says thank you to wysota for this useful post:


  9. #7
    ucomesdag Guest

    Default Re: Stylesheet and QFontMetrics

    Quote Originally Posted by wysota View Post
    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!

  10. #8
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Stylesheet and QFontMetrics

    Hi,

    I am facing similar problem, pl. see http://www.qtcentre.org/threads/2731...ached-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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.