PDA

View Full Version : Getting Standard Fonts



Radek
14th August 2013, 16:02
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

ChrisW67
15th August 2013, 02:54
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.

Radek
15th August 2013, 07:45
The Debian KDE solution: The KGlobalSettings KDE object contains static members returning the needed QFonts:



QFont KGlobalSettings::generalFont() const; // the general proportional font
QFont KGlobalSettings::fixedFont() const; // the fixed pitch font
QFont KGlobalSettings::toolbarFont() const; // the toolbar font
QFont KGlobalSettings::menuFont() const; // the menu font
QFont KGlobalSettings::windowTitleFont() const; // the titlebar font
QFont KGlobalSettings::largeFont() const; // the 48 pts font
QFont KGlobalSettings::smallestReadableFont() const; // the smallest readable font


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



#include <kglobalsettings.h>
#include <text.hpp>


bool Text::FontConf( const fontconf_t& fc ) // Text is "public QPlainTextEdit"
{
QColor co(currfmt.foreground().color()); // currfmt is QTextCharFormat of the current text char format
int fs = currfmt.font().pointSize();
bool bo = (currfmt.fontWeight() == QFont::Bold);
bool bb = false;

if( currfmt.fontFixedPitch() != fc.isfixed )
{
currfmt.setFont(fc.isfixed ? KGlobalSettings::fixedFont() : KGlobalSettings::generalFont());
currfmt.setFontFixedPitch(fc.isfixed);
bb = true;
}

if( fs != fc.psize )
{
currfmt.font().setPointSize(fc.psize);
bb = true;
}

if( bo != fc.isbold )
{
currfmt.setFontWeight(fc.isbold ? QFont::Bold : QFont::Normal);
bb = true;
}

if( currfmt.fontItalic() != fc.isitalic )
{
currfmt.setFontItalic(fc.isitalic);
bb = true;
}

if( co != fc.color )
{
QBrush newcolor(fc.color);

currfmt.setForeground(newcolor);
bb = true;
}

if( bb ) setCurrentCharFormat(currfmt);

return bb;
}


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.