Qt6: QFontDatabase does not addApplicationFont
-
I cannot addApplicationFont. It allways returns -1.
I tried simple example at: https://github.com/eyllanesc/stackoverflow/tree/master/questions/55585443. The code is:#include <QtWidgets> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w; QVBoxLayout *lay = new QVBoxLayout{&w}; QComboBox combo; QLabel label{"Stack Overflow"}; lay->addWidget(&combo); lay->addWidget(&label); QObject::connect(&combo, &QComboBox::currentTextChanged, [&label](const QString & name){ label.setFont(QFont(name)); }); QDir directory(":/Roboto"); for(const QFileInfo & info: directory.entryInfoList()){ int id = QFontDatabase::addApplicationFont(info.absoluteFilePath()); if(id != -1){ QStringList font_families = QFontDatabase::applicationFontFamilies(id); if(!font_families.isEmpty()) combo.addItem(font_families.first()); } } w.show(); return a.exec(); }
My QT version is 6. Most of found tutorials, recomendations and answers are about older versions.
Thanks
-
Hi,
Does it work if you use Qt 5 ?
-
@achak said in Qt6: QFontDatabase does not addApplicationFont:
can check the example on Qt 5 but this does not help me with my project
But it would solve the question if it's a regression or not.
-
@Christian-Ehrlicher What is simplest way for check old version.
-
@jsulm said in Qt6: QFontDatabase does not addApplicationFont:
@achak Install Qt 5.15.2 and build your app using it.
Unfortunatelly, by our policy I cannot install another version QT.
The QT 5.15 is unsupported from 69/5/2021. If the sample works on 5.15, how it will support the issue? -
@achak As @Christian-Ehrlicher wrote: checking whether or not it works with Qt5 is just to see whether there is a regression in Qt6. Means: if it works in Qt5 but not in Qt6 then something was broken in Qt6.
-
In addition, you might want to enable the
qt.qpa.fonts
category and see whether it prints any helpful debug messages on why your fons are not loaded: https://doc.qt.io/qt-6/qloggingcategory.html#configuring-categories -
@kkoehne said in Qt6: QFontDatabase does not addApplicationFont:
qt.qpa.fonts
Thanks!!
Following is output:qt.qpa.fonts: systemDefaultFont QFont(Segoe UI,9,-1,5,400,0,0,0,0,0,0,0,0,0,0,1) qt.qpa.fonts: QWindowsFontDatabase Clear type: true gamma: 1.2 qt.qpa.fonts: systemDefaultFont QFont(Segoe UI,9,-1,5,400,0,0,0,0,0,0,0,0,0,0,1) qt.qpa.fonts: "Segoe UI" qt.qpa.fonts: "addFontToDatabase Segoe UI 0 TTF=1 TRUETYPE scalable=1 Size=65535 Style=0 Weight=400 stretch=100" qt.qpa.fonts: "addFontToDatabase Segoe UI 0 TTF=1 TRUETYPE scalable=1 Size=65535 Style=0 Weight=700 stretch=100" qt.qpa.fonts: "addFontToDatabase Segoe UI 0 TTF=1 TRUETYPE scalable=1 Size=65535 Style=1 Weight=700 stretch=100" qt.qpa.fonts: "addFontToDatabase Segoe UI 0 TTF=1 TRUETYPE scalable=1 Size=65535 Style=1 Weight=400 stretch=100" qt.qpa.fonts: createEngine "Segoe UI" 9 pt hintingPreference= QFont::PreferDefaultHinting color= false 96 dpi useDirectWrite= false qt.qpa.fonts: QWindowsFontEngine "Segoe UI" -12 qt.qpa.fonts: fontEngine FONTDEF QFontDef(Family="Segoe UI", pointsize=9, pixelsize=12, styleHint=5, weight=400, stretch=100, hintingPreference=0) 0x2ce0360 0x0
-
Hi, unfortunately the output isn't as informative as I have hoped :(
I just gave https://github.com/eyllanesc/stackoverflow/tree/master/questions/55585443 a test drive; the example works for me with Qt 6.2.2, both with MSVC and MinGW.
Can you try also with Qt 6.2.2?
-
I got this working in 6.2.3 recently. At first I was tripped up by the qrc path. Note that if you have something like:
qml.files = $$files(*.qml) qml.prefix = /qml fonts.files = $$files(fonts/*) fonts.prefix = / # will automatically get directory name "fonts" as prefix RESOURCES += qml fonts
in your
.pro
file, then Qt will expect to find qml files in the top-level directory, but their qrc path will have a "/qml" prefix. On the other hand it will expect to find ttf files in the fonts subdirectory, and still need to be accessed using a "/fonts" prefix.So with the
/pro
above, this works for me inmain.cpp
:QFontDatabase::addApplicationFont(":/fonts/Arial.ttf"); QFontDatabase::addApplicationFont(":/fonts/Arial Italic.ttf"); QFontDatabase::addApplicationFont(":/fonts/Arial Bold.ttf"); QFontDatabase::addApplicationFont(":/fonts/Arial Bold Italic.ttf");
returning 0, 1, 2 and 3 respectively.
Also remember to clean your build before re-building, because the qrc resource files might be pre-built by
qmake
.PS. I'm not recommending anyone distribute Arial with your app. Do you own legal research.