QFontDatabase::addApplicationFont return -1
-
My ( first) app on Windows using QT Creator 5.15 . I try load a font from the Google collection.
I put ttf files into C:/try/gui_0/fonts/SourceSansPro/ folder. Add following line into qrc file:<file alias="SSP_Regular.ttf">fonts/SourceSansPro/SourceSansPro-Regular.ttf</file>```
I entered following c++ code into main function:
int fnt_id = QFontDatabase::addApplicationFont(":/fonts/SourceSansPro/SourceSansPro-Regular.ttf"); int ali_id = QFontDatabase::addApplicationFont(":/fonts/SourceSansPro/SSP_Regular.ttf"); int shrt_id = QFontDatabase::addApplicationFont(":/SSP_Regular.ttf"); int an_id = QFontDatabase::addApplicationFont("SSP_Regular.ttf"); int qrc_id = QFontDatabase::addApplicationFont("qrc:/fonts/SourceSansPro/SourceSansPro-Regular.ttf"); int fle_id = QFontDatabase::addApplicationFont("C:/try/gui_0/fonts/SourceSansPro/SourceSansPro-Regular.ttf");
Each line bellow returns -1.
Whats a problem? -
Only the last one can work - the rest is wrong.
Please make sure that the file is really there and readableQFileInfo fi("C:/try/gui_0/fonts/SourceSansPro/SourceSansPro-Regular.ttf"); if (!fi.exists() || !fi.isReadable()) { qWarning() << "Wrong filename"; } else { int fle_id = QFontDatabase::addApplicationFont(fi.absoluteFilePath()); qDebug() << fle_id ; }
-
@Christian-Ehrlicher said in QFontDatabase::addApplicationFont return -1:
QFileInfo fi("C:/try/gui_0/fonts/SourceSansPro/SourceSansPro-Regular.ttf");
if (!fi.exists() || !fi.isReadable()) {
qWarning() << "Wrong filename";
} else {
int fle_id = QFontDatabase::addApplicationFont(fi.absoluteFilePath());
qDebug() << fle_id ;
}Debug output is -1. The font file is exist and readable
-
Then enable the debug logging for
qt.qpa.fonts
(see detailed description here: https://doc.qt.io/qt-5/qloggingcategory.html#details ) and see what it prints to your console.