[Interest] Qt and font weight on Linux

Roland Hughes roland at logikalsolutions.com
Sat Jan 4 12:47:51 CET 2020


A quick test on KDE Neon (a YABU) shows it appears to be a problem with 
all Mono fonts being treated as fixed width when monospaced != fixed 
width. Fixed width fonts have a cpi not point size measurement. By 
historical definition the only way to get "bold" was to print the same 
character twice in the same space on paper. (If I remember correctly, 
phosphorus terminals increased the voltage to make the character brighter.)

Here is a quick and dirty I used to try several other Mono fonts on my 
system, all with the same result. The font name is a constant string at 
the top of mainwindow.cpp so it can quickly be changed and tested.

When run with "Ubuntu Mono" only one name shows up in list.

05:42:40: Debugging starts Font: "Ubuntu Mono" 05:42:47: Debugging has 
finished

Just Ubuntu shows

05:43:27: Debugging starts Font: "Ubuntu" Font: "Ubuntu Condensed" Font: 
"Ubuntu Light" Font: "Ubuntu Mono" 05:43:36: Debugging has finished

If someone has Ubuntu 16 & 18 installed somewhere with the pre-packaged 
Qt it would be interesting to see if this is "new feature" or just newly 
discovered.

===

QT       += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 
CONFIG += c++11 # The following define makes your compiler emit warnings 
if you use # any Qt feature that has been marked deprecated (the exact 
warnings # depend on your compiler). Please consult the documentation of 
the # deprecated API in order to know how to port your code away from 
it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail 
to compile if it uses deprecated APIs. # In order to do so, uncomment 
the following line. # You can also select to disable deprecated APIs 
only up to a certain version of Qt. #DEFINES += 
QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs 
deprecated before Qt 6.0.0 SOURCES += \     main.cpp \     
mainwindow.cpp HEADERS += \     mainwindow.h FORMS += \     
mainwindow.ui # Default rules for deployment. qnx: target.path = 
/tmp/$${TARGET}/bin else: unix:!android: target.path = 
/opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target

===

#include "mainwindow.h" #include <QApplication> int main(int argc, char 
*argv[]) {     QApplication a(argc, argv);     MainWindow w;     
w.show();     return a.exec(); }

=====

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFont> 
#include <QFontDatabase> #include <QDebug> static QString fontName = 
"Ubuntu Mono"; MainWindow::MainWindow(QWidget *parent)     : 
QMainWindow(parent)     , ui(new Ui::MainWindow) {     
ui->setupUi(this);     listFonts();     QFont font(fontName, 14, 
QFont::Normal);     ui->label->setFont(font);     
ui->label->setText("Text with normal weight");     QFont font2(fontName, 
14, QFont::Bold);     ui->label_2->setFont(font2);     
ui->label_2->setText("Text with bold weight");     QFont font3(fontName, 
14, QFont::Bold, true);     ui->label_3->setFont(font3);     
ui->label_3->setText("Text with bold italic");     QFont font4(fontName, 
14, QFont::Normal, true);     ui->label_4->setFont(font4);     
ui->label_4->setText("Text with normal italic"); } 
MainWindow::~MainWindow() {     delete ui; } void 
MainWindow::listFonts() {     QFontDatabase fontDb;     QStringList 
fontFamilies = fontDb.families();     for (QString family : 
fontFamilies)     {         if (family.contains(fontName))         { 
             qDebug() << "Font: " << family;         }     } }

===

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 
QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE 
class MainWindow : public QMainWindow {     Q_OBJECT public:     
MainWindow(QWidget *parent = nullptr);     ~MainWindow(); private:     
void listFonts();     Ui::MainWindow *ui; }; #endif // MAINWINDOW_H

===

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> 
  <class>MainWindow</class>  <widget class="QMainWindow" 
name="MainWindow">   <property name="geometry">    <rect>     <x>0</x> 
     <y>0</y>     <width>800</width>     <height>600</height>    </rect> 
   </property>   <property name="windowTitle">    
<string>MainWindow</string>   </property>   <widget class="QWidget" 
name="centralwidget">    <widget class="QLabel" name="label">     
<property name="geometry">      <rect>       <x>40</x>       <y>40</y> 
       <width>201</width>       <height>16</height>      </rect>     
</property>     <property name="text">      <string>TextLabel</string> 
     </property>    </widget>    <widget class="QLabel" name="label_2"> 
     <property name="geometry">      <rect>       <x>40</x>       
<y>90</y>       <width>211</width>       <height>16</height>      
</rect>     </property>     <property name="text">      
<string>TextLabel</string>     </property>    </widget>    <widget 
class="QLabel" name="label_3">     <property name="geometry">      
<rect>       <x>40</x>       <y>150</y>       <width>211</width>       
<height>16</height>      </rect>     </property>     <property 
name="text">      <string>TextLabel</string>     </property>    
</widget>    <widget class="QLabel" name="label_4">     <property 
name="geometry">      <rect>       <x>40</x>       <y>220</y>       
<width>201</width>       <height>16</height>      </rect>     
</property>     <property name="text">      <string>TextLabel</string> 
     </property>    </widget>   </widget>   <widget class="QMenuBar" 
name="menubar">    <property name="geometry">     <rect>      <x>0</x> 
      <y>0</y>      <width>800</width>      <height>28</height>     
</rect>    </property>   </widget>   <widget class="QStatusBar" 
name="statusbar"/>  </widget>  <resources/>  <connections/> </ui>

On 1/4/20 5:00 AM, Benjamin TERRIER wrote:
> Hi,
>
> I have an issue on a computer running KUbuntu 19.10.
>
> Basically the issue is that Qt does not handle properly the font weights.
>
> For instance for "Ubuntu Mono" I have the Regular and Bold fonts installed
> and they are properly registered in fontconfig:
>
>      fc-match "Ubuntu Mono"
>      UbuntuMono-R.ttf: "Ubuntu Mono" "Regular"
>
>      fc-match "Ubuntu Mono:Bold"
>      UbuntuMono-B.ttf: "Ubuntu Mono" "Bold"
>
> However, Qt applications fail to change the weight. Be it in my own
> software or in Qt Creator, be it using Qt official packages or Ubuntu
> provided packages, with widgets or Qt Quick: the font always appears as
> "Regular". Using `QFontInfo::styleName()`, I can
> confirm that it is always the "Regular" variant that is used.
>
> When trying with the "Ubuntu" family (instead of "Ubuntu Mono") for which I
> have all variations installed (Light, Thin, Regular, ...),
> I am only able to have Regular and Bold texts.
>
> I have tried on other computers (Windows, KDE Neon and Ubuntu 18.04) and
> for all of them it is working fine.
>
> Also when using Kate I am able to use Ubuntu Mono in bold.
>
> So I am wondering what can be causing this kind of issue?
> Could it be some incompatibility between Qt official packages and the
> latest Ubuntu? Bu then using the distro packages should solve the problem.
> Or could it be a system configuration issue? But then Kate and fc-match
> shouldn't work either.

-- 
Roland Hughes, President
Logikal Solutions
(630)-205-1593

http://www.theminimumyouneedtoknow.com
http://www.infiniteexposure.net
http://www.johnsmith-book.com
http://www.logikalblog.com
http://www.interestingauthors.com/blog



More information about the Interest mailing list