Results 1 to 6 of 6

Thread: Setting the Font in a Custom Widget

  1. #1
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Setting the Font in a Custom Widget

    Hi,

    I'm currently developing a custom widget (a meter that displays realtime input). I need to draw a scale on the meter. So far I've used the following:

    Qt Code:
    1. QFont font("Times", 8, QFont::Normal, true);
    2. setFont(font);
    3. for(unsigned int i = 0; i < 11; ++i) {
    4. painter.drawLine(15, 20*i, 19, 20*i);
    5. painter.drawText(3, 20*i + 3, QString::number(i*6));
    6. }
    To copy to clipboard, switch view to plain text mode 

    I'm not happy with the font - how can I work out what fonts are available?

  2. #2
    Join Date
    May 2011
    Posts
    239
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60
    Thanks
    4
    Thanked 35 Times in 35 Posts

    Default Re: Setting the Font in a Custom Widget

    How did you miss QFontDatabase? http://doc.qt.nokia.com/latest/qfontdatabase.html

  3. #3
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Setting the Font in a Custom Widget

    Thanks!

    One thing about the code I posted - it seems that even if I change the font name, the font shown in the widget doesn't change. Am I missing something?

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

    Default Re: Setting the Font in a Custom Widget

    set the font at the QPainter object you are using to draw the text. If you call setFont outside the paint event, then it will be set the next time the paintEvent method will be called.

  5. #5
    Join Date
    Jun 2010
    Posts
    30
    Thanks
    7

    Default Re: Setting the Font in a Custom Widget

    The code I posted is called inside the widget's paintEvent method.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Setting the Font in a Custom Widget

    You can set the font to the painter

    Qt Code:
    1. QFont font("Times", 8, QFont::Normal, true);
    2. painter->setFont(font);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 10
    Last Post: 10th November 2010, 04:12
  2. How to using/setting the font caching
    By iceman in forum Newbie
    Replies: 5
    Last Post: 28th July 2010, 06:35
  3. QCompleter once again (setting a font)
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2007, 13:42
  4. Replies: 3
    Last Post: 30th January 2007, 08:35
  5. Reg - Font setting QListBox
    By lawrence in forum Newbie
    Replies: 1
    Last Post: 23rd December 2006, 21:07

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
  •  
Qt is a trademark of The Qt Company.