Change default application font - qt

I have a Qt application that parses some JSON files and outputs its contents. I want the output to be in a monospaced font and the simplest way to do that is changing the default font of the entire application to a monospace. How do I do that in Qt?

Simply use the setFont() method on the QApplication or QWidget:
QFont font("Courier New");
font.setStyleHint(QFont::Monospace);
QApplication::setFont(font);
Note the setStyleHint(QFont::Monospace) line: it ensures that even if the specified font family is not present in the system, another suitable monospace font will be used.
Also, in my opinion it's better to set font for a certain widget, not the whole application: this gives you a more structured code for your UI in case of its expansion. However, this is still a matter of a design, of course.

The only way I have figured out to change the font for a whole application in Qt is with stylesheets. For PyQt in the application's init class you can call self.setStyleSheet('QWidget {font: "Roboto Mono"}'). Due to the cascading nature of stylesheets this will set the font of all widgets to Roboto Mono.
Just setting QApplication.setFont(font) has not always worked for me. Sometimes deeply nested child widgets do not seem to respect the font, such as headers in QTreeView.

Related

Rendered text looks very different in Qt and Blink, although same font is set

The upper font is from a QLabel with QFont("Arial").
The lower font is rendered with QWebEngine, e.g. Chromium/Blink backend, using css
font-family: Arial, Helvetica, sans-serif;
font-size: 8pt;
Is there a way to make the blink one look as dark and sharp as the Qt one? It looks blurry and as both texts in the app are placed next to each other, it does not look good enough.
(Qt5.8 msvc 2015 on windows 8.1)
Edit
The reason is that chromium since version 52 did drop GDI support. In former version GDI rendering could be activated by disabling the DirectWrite flag. So basically I need to recompile Qt with a different chromium backend. Or perhaps with Cent Browser because it still supports that flag despite using newest chromium.
First get the font family using QFontInfo::family().
This function returns matched family name of windows system font.
QFont f("Arial");
QFontInfo info(f);
QString family = info.family();
In simple terms "QFontInfo object is used to determine the attributes of the font actually used in the window system."
Use the family name returned by QFontInfo and set the same for QWebEngine by QWebEngineSettings::setFontFamily or using CSS.
useful links:
http://doc.qt.io/qt-5/qfontinfo.html#family
http://doc.qt.io/qt-5/qwebenginesettings.html#setFontFamily

Why does Chrome render newly declared web fonts in italic by default?

It's not the answer to the question above but I've put work I've done on this below as a bit of back ground so's not to rub people up the wrong way!
Finally figured out how to stop Chrome rendering newly declared web fonts in italic by default. I'm sure it's something to do with declaring a new web font on block level elements and having various options on your font. You need to reset your font not with font-style:normal; but font-weight:num; as dictated in your font settings (if using Google Fonts). Why it overrides the font style in the first place, I'm all ears!

Relative font sizes in QT or the most portable way to define them

Coming from a Web Development background I am now into QT Applications development.
Working with QFonts I have seen that I have only two options apparently, to define fonts sizes in QT; either by pixel size, or point size.
When making web layouts I am used to define all my fonts in a relative way... using em units which refer to the inherited font size, where 1em equals the font-size of the container element, 0.5em 50% the font-size, 1.5em 50% bigger, and so on.
I am worried about portability and device/OS idenpendence of my applications. Is there any better way to define these fonts or should I instead stick to one of these methods?
If possible, can I inherit font-sizes and define sizes in relative units such as ems or percentages?
Depending on how you design your application, I'm afraid that as well as worrying about font size, you will also need to be very careful about platform-specific fonts.
For example, I've just run Qt Designer, inside Qt Creator 2.3.0 (Based on Qt 4.7.4) - so pretty much the latest stable code, and done the following:
Created a new Qt Designer form (i.e. a .ui file)
Made it an empty Widget
Added a QTextEdit (this is called "Text Edit" in Designer's "Input Widgets" section)
Double-clicked on the QTextEdit, to open its properties
Viewed its Source
This is what I see:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html>
Note the use of Windows-specific font, and a very hard-coded size, before I've even made any changes to the content: font-family:'MS Shell Dlg 2'; font-size:7.8pt;
I've previously reported this to Qt's user support, and been told (via the status of the bug) that there there are no plans to fix it. (See Edit below, for info on these bugs)
So at work, every release, we have to search through all our .ui files, to make sure that no platform-specific font info has crept in to any .ui file since the previous release.
(A year or two ago, I ran Designer on each of Mac, Windows and Linux, and showed that each version generated its own platform-specific font info like this, and very different default font sizes. Each failed to display correctly on the other two platforms! The Mac text was way too large on the other two, and the other two were way too small on the Mac)
I really like Qt a lot, but I find this lack of platform independence in something so basic as its UI designer rather frustrating.
Edit - bug info from TrollTech, July 2009
I've found the info from when I reported '.ui cross-platform problems - portability of fonts and sizes'. I don't know how these old references numbers, from TrollTech in July 2009, translate to current Qt issue-tracking: I'm hoping someone else might be able to update the links, to current working ones:
TrollTech support ticket: N258723
Want to specify relative sizes for QLabels, with using platform-specific fixed sizes:
[broken link] http://www.trolltech.com/developer/task-tracker/index_html?method=entry&id=241089
How can we stop Qt Designer putting in platform-specific font names and font sizes in future?
[broken link] http://www.qtsoftware.com/developer/task-tracker/index_html?method=entry&id=153335
It is not currently possible. In my opinion, the most portable way would be using pts. since they are less device-dependent than pxs.
in qt3, the next code works at least for fixed fonts:
QFont f=font();
int psz=f.pixelSize();
if (psz>0) f.setPixelSize(psz*6/5);
else f.setPointSizeFloat(f.pointSizeFloat()*1.2);

What is the meaning of term "css font stack"? Has the meaning of the term changed?

I'm using Dreamweaver for many years which gives some suggestions to put font-families in CSS.
Is it not a font-stack? What is new in the term "CSS Font Stack"
What I know is, that one defines multiple font families to keep the typography consistent if any font is not available in system.
You are correct - You define multiple fonts and the browser will simply choose the first one in the list that you have on your system.
A font stack allows you to define multiple fonts to essentially provide a better experience for users.
Using the CSS font-family property, you can define multiple fonts, like so:
font-family: Arial,Liberation Sans,DejaVu Sans,sans-serif;
This reads from left to right, if the user does not have Arial then font-family will fallback on Liberation all the way down to the most basic sans-serif (select any sans-serif font if the aforementioned fonts are not found).
font stack, is just a list of fonts (of font families), if the first is not available to the browser, then the second is used etc..
your picture shows 6 font stacks, you could choose from
there's nothing new, this has been the standard way to set fonts for some years now.
You are correct in your thinking. The pulldown menu has 'Web Safe' font stacks to choose from. If you use any of them your text should render fairly consistently on all devices and on all browsers.
The trend now is to use Google Fonts or some other imported or loaded fonts and thereby control what is rendered. Even then it is recommended to also include one or more alternative standard fonts in a font stack in case your visitors cannot load Google Fonts or the other choices you provide.

How does the font-family property work in CSS?

How does the font-family property work in CSS? Why is more than one font used? Isn't only one font used at a time by the browser?
The font-family property holds several font names to provide a "fallback" system.
The browser tries each font family in the order that they are listed; if the browser does not support the first font, it tries the next font, and so on, down the list. That's why it's important that at least the last font in the list be a generic font family that is guaranteed to be universally available. There is no guarantee that the fonts you have loaded on your computer when you design the web page will be loaded on your visitor's computers—fonts are generally handled client-side, rather than server-side.
A common declaration might look like this:
font-family:Georgia,"Times New Roman",serif;
The "Georgia" font will be used if it is available. If not, the browser will attempt to fall back to "Times New Roman". If it can't find that font either, it will use a generic serif font.
For more technical information, I suggest reading the Fonts specification from the W3C.
To expand on what cody said:
When you look at a web page through a browser, your browser looks at the css and sees what fonts to use. Then it checks this list against the list of fonts that your computer has installed; the first one that matches is the one that gets used. Fonts are client-side, not server-side, and if you don't have the font that the css specifies, your browser falls back either to the next font specified or a default font.

Resources