Results 1 to 3 of 3

Thread: Getting Standard Fonts

  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Getting Standard Fonts

    I would like to use standard fonts in my app. I need the standard (in KDE, configured in the System Settings) proportional and fixed pitch fonts. I haven't detected a Qt-way, how to get proportional and fixed QFonts corresponding to the ones configured in default font settings - or some info which allows getting these QFonts. Perhaps, I am overlooking something.

    The default font seems to be the "desktop font". It is, perhaps always, a proportional font but suppose purely theoretically that a user configured his desktop so that the desktop font is fixed pitch. What font do I get as a default font? And How to get the fixed pitch font? QTextCharFormat::setFontFixedPitch(true) seems to be ignored in my app. In fact, no wonder, fonts are usually either proportional or fixed pitch.

    Debian Wheezy, KDE, Qt 4.8.4

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting Standard Fonts

    If you want to know what KDE settings have stored then you need to ask KDE. You might be able to rely on using QSettings to read the "~.kde4/share/config/kdeglobals" file for this information but I do not know how stable this would be and there may be a "correct" KDE API to use.

    The default font in a Qt application UI is a suitable generic font for the platform. The formatting of characters in a QTextDocument is a different matter. You can QTextDocument::setDefaultFont() any way you like and set the font used for particular characters using QTextCharFormat::setFont(). As you point out, calling setFixedPitch() on a font with only proportional spacing will probably not work, but create a QFont with a suitable font and setting that should. You can inspect all the available fonts in the system using QFontDatabase.

  3. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Getting Standard Fonts

    The Debian KDE solution: The KGlobalSettings KDE object contains static members returning the needed QFonts:

    Qt Code:
    1. QFont KGlobalSettings::generalFont() const; // the general proportional font
    2. QFont KGlobalSettings::fixedFont() const; // the fixed pitch font
    3. QFont KGlobalSettings::toolbarFont() const; // the toolbar font
    4. QFont KGlobalSettings::menuFont() const; // the menu font
    5. QFont KGlobalSettings::windowTitleFont() const; // the titlebar font
    6. QFont KGlobalSettings::largeFont() const; // the 48 pts font
    7. QFont KGlobalSettings::smallestReadableFont() const; // the smallest readable font
    To copy to clipboard, switch view to plain text mode 

    Add the libkdeui library to the .pro file (I have noticed that the library is kdeui on Ubuntu). My current font setting procedure:

    Qt Code:
    1. #include <kglobalsettings.h>
    2. #include <text.hpp>
    3.  
    4.  
    5. bool Text::FontConf( const fontconf_t& fc ) // Text is "public QPlainTextEdit"
    6. {
    7. QColor co(currfmt.foreground().color()); // currfmt is QTextCharFormat of the current text char format
    8. int fs = currfmt.font().pointSize();
    9. bool bo = (currfmt.fontWeight() == QFont::Bold);
    10. bool bb = false;
    11.  
    12. if( currfmt.fontFixedPitch() != fc.isfixed )
    13. {
    14. currfmt.setFont(fc.isfixed ? KGlobalSettings::fixedFont() : KGlobalSettings::generalFont());
    15. currfmt.setFontFixedPitch(fc.isfixed);
    16. bb = true;
    17. }
    18.  
    19. if( fs != fc.psize )
    20. {
    21. currfmt.font().setPointSize(fc.psize);
    22. bb = true;
    23. }
    24.  
    25. if( bo != fc.isbold )
    26. {
    27. currfmt.setFontWeight(fc.isbold ? QFont::Bold : QFont::Normal);
    28. bb = true;
    29. }
    30.  
    31. if( currfmt.fontItalic() != fc.isitalic )
    32. {
    33. currfmt.setFontItalic(fc.isitalic);
    34. bb = true;
    35. }
    36.  
    37. if( co != fc.color )
    38. {
    39. QBrush newcolor(fc.color);
    40.  
    41. currfmt.setForeground(newcolor);
    42. bb = true;
    43. }
    44.  
    45. if( bb ) setCurrentCharFormat(currfmt);
    46.  
    47. return bb;
    48. }
    To copy to clipboard, switch view to plain text mode 

    It works in simple tests on fixed font. Possible minor problems ahead: what all is reset by setFont()? In the other words, what all do I need to set unconditionally in FontConf() after setFont() (point size, bold/italic, etc.)? Note: the FontFixedPitch() seems to be only a flag unrelated to the font. If I set a fixed font, the flag returned by FontFixedPitch() does not change. Therefore, setFontFixedPitch() along with the font to keep consistency.

Similar Threads

  1. Adding Arabic fonts and CJK (China Japan Korean) fonts
    By kishore7771 in forum Qt Programming
    Replies: 0
    Last Post: 30th July 2013, 18:33
  2. standard QT libraies
    By GrahamLabdon in forum Newbie
    Replies: 2
    Last Post: 21st October 2010, 12:37
  3. About Fonts
    By QbelcorT in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2009, 17:58
  4. How to use qpf fonts
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th September 2008, 07:56
  5. Replies: 2
    Last Post: 2nd June 2008, 08:45

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.