Results 1 to 6 of 6

Thread: Howto get font of widget set by stylesheet

  1. #1
    Join Date
    Jun 2013
    Location
    Germany
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Howto get font of widget set by stylesheet

    Hi,

    I want to get the font of a QWidget which is set by a stylesheet. The font() method always returns the global system font or the font set by setFont, but not the font which is set by setStyleSheet() and is used for painting in the widget. I need the font to do some calculations based on the fontsize.

    Example code

    Qt Code:
    1. FontTest::FontTest(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6. QTextEdit *edit = new QTextEdit(this);
    7. setCentralWidget(edit);
    8. edit->setStyleSheet("font-family: Arial;font-style: normal;font-size: 12pt;font-weight: bold;");
    9. edit->setPlainText("Hello World");
    10. qDebug() << "Font: " << edit->font().family() << edit->font().pointSize();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Painting is done with Arial, 12pt but qDebug() prints "Font: "MS Shell Dlg 2" 8 "

    This is just a small example. In real life I have no knowledge about the font of the stylesheet because it comes from a qss file supplied at runtime and is set at application level.

    I use Qt 4.8.1 under Win7

    Thanks for any help

    Helmut

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Howto get font of widget set by stylesheet

    You have to specify the element name like this:-
    Qt Code:
    1. edit->setStyleSheet("QTextEdit{font-family: Arial;font-style: normal;font-size: 12pt;font-weight: bold;};");
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Jun 2013
    Location
    Germany
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Howto get font of widget set by stylesheet

    Thanks for the answer but that's not the problem.

    Qt Code:
    1. qDebug() << "Font: " << edit->font().family() << edit->font().pointSize();
    To copy to clipboard, switch view to plain text mode 

    still prints "Font: "MS Shell Dlg 2" 8 ".

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Howto get font of widget set by stylesheet

    Just a guess, maybe the font is not set before the edit shows up. So just for checking. Move the qDebug in a custom slot and call it after the window has shown up. (E.g. use QTimer::singleShot() for that)

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

    swaczin (8th July 2013)

  6. #5
    Join Date
    Jun 2013
    Location
    Germany
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Howto get font of widget set by stylesheet

    Yes, I get the font after the edit is shown.
    Thanks!

  7. #6
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Howto get font of widget set by stylesheet

    Warning: resurrected thread.

    - - - - -

    I just faced the same problem. I wanted to point out that instead of using QTimer::singleShot, you can simply call ensurePolished() right before querying any elements of the widget style that are set by the custom style sheet, like a custom font.

    Like Lykurg guessed, it's exactly what happens the first time the widget is shown, it is polished.

    Additionally, if you're interested in setting a 'minimum width' for a QLineEdit so that it always fits the placeholder text, you can use the following.
    Subclass QLineEdit and declare a public function 'void setPlaceholderText( const QString& placeholder ).' This will shadow the original function from QLineEdit.
    Then the code for the shadowing function is:
    Qt Code:
    1. #include <QStyleOptionFrameV2>
    2.  
    3. void SubclassedQLineEdit::setPlaceholderText( const QString& placeholder )
    4. {
    5. if ( placeholder.isEmpty() )
    6. return;
    7.  
    8. QLineEdit::setPlaceholderText( placeholder ); // Original function.
    9.  
    10. // Set a minimum size if the widget has valid placeholder text, so
    11. // the placeholder text isn't truncated at the end.
    12.  
    13. ensurePolished(); // Updates the widget from the default font to the style-sheet font.
    14.  
    15. initStyleOption( &frame );
    16. const QSize contentsSize( fontMetrics().width( text ), fontMetrics().height() );
    17. const int width = style()->sizeFromContents( QStyle::CT_LineEdit, &frame, contentsSize, this ).width();
    18.  
    19. setMinimumWidth( width );
    20. }
    To copy to clipboard, switch view to plain text mode 
    There's always some empty space left, but the line edit is guaranteed to display all the placeholder text without truncating it (as in, "placehold...").
    Last edited by Kryzon; 1st October 2015 at 00:30.

Similar Threads

  1. Problem with setting font via stylesheet
    By jamadagni in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2012, 11:19
  2. Replies: 2
    Last Post: 2nd November 2010, 02:43
  3. Replies: 0
    Last Post: 12th August 2010, 17:05
  4. Replies: 4
    Last Post: 14th June 2010, 16:01
  5. Animated graphical widget in systray. howto?
    By Tetractys in forum Newbie
    Replies: 2
    Last Post: 7th October 2009, 00:51

Tags for this Thread

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.