Results 1 to 5 of 5

Thread: Problems with StyleSheet and QFontMetrics (see attached program)

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

    Default Problems with StyleSheet and QFontMetrics (see attached program)

    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
    Attached Files Attached Files

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

    Default Re: Problems with StyleSheet and QFontMetrics (see attached program)

    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.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with StyleSheet and QFontMetrics (see attached program)

    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.

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

    Default Re: Problems with StyleSheet and QFontMetrics (see attached program)

    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.

  5. #5
    Join Date
    May 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with StyleSheet and QFontMetrics (see attached program)

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

Similar Threads

  1. Stylesheet and QFontMetrics
    By ucomesdag in forum Qt Programming
    Replies: 7
    Last Post: 18th January 2010, 08:45
  2. QFontMetrics.height()
    By Caius Aérobus in forum Qt Programming
    Replies: 8
    Last Post: 4th April 2009, 18:36
  3. Replies: 2
    Last Post: 26th March 2009, 09:43
  4. QFontMetrics and HTML tags
    By vonCZ in forum Newbie
    Replies: 1
    Last Post: 14th August 2008, 13:13
  5. QFontMetrics, i can't understand
    By pakulo in forum Qt Programming
    Replies: 11
    Last Post: 24th June 2007, 18:59

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.