Embedding custom fonts in Qt 4.8.4
Hello,
we have problems embedding fonts in our application using QFontDatabase::addApplicationFontFromData:
Code:
list << "qdsFont1.ttf" << "qdsFont2.ttf" << "qdsFont3.ttf" << "qdsFont4.ttf";
int fontID(-1);
bool fontWarningShown(false);
for (QStringList::const_iterator constIterator
= list.
constBegin(); constIterator
!= list.
constEnd();
++constIterator
) { QFile res
(":/polices/" + *constIterator
);
if (res.
open(QIODevice::ReadOnly) == false) { if (fontWarningShown == false) {
fontWarningShown = true;
}
} else {
fontID
= QFontDatabase::addApplicationFontFromData(res.
readAll());
if (fontID == -1 && fontWarningShown == false) {
fontWarningShown = true;
}
for (QStringList::const_iterator constIt
= fntList.
constBegin(); constIt
!= fntList.
constEnd();
++constIt
) { qDebug() << argv[0] << "QFont=" << *constIterator << "QFont=" << *constIt;
}
}
}
a.
setFont(QFont("qdsFont1"),
13);
#define FUNCIDENT "main"
int ps=13;
qDebug() << FUNCIDENT << "font=" << str << "pointsize=" << ps;
qDebug() << FUNCIDENT << "font=" << info.family() << "pixelsize=" << info.pointSize() << info.pixelSize() << info.weight() << info.exactMatch();
The fonts are stored in a qt resource file.
a.setFont() does not work. ( QApplication a(argc, argv); )
After creating the testFont QFontInfo allways tells me, that Arial is used, not my own font.
Qt 4.8.4 / Linux
created with
Code:
/opt/qt-4.8.4-src/configure --prefix=/opt/qt-4.8.4-i386 --platform=linux-g++-32 -no-glib
-nomake demos -nomake examples -confirm-license -opensource -no-openssl -no-opengl -no-nis
-xinput -no-cups -no-dbus -no-webkit -xrender -fontconfig
ldd shows me that the application uses these libs:
Quote:
linux-gate.so.1 => (0xffffe000)
libqwt.so.6 => /opt/qwt-6.0.2/lib/libqwt.so.6 (0xb7e40000)
libqdswidget.so => /home/qdsemu/qdsemu/home/lib/libqdswidget.so (0xb7d0a000)
libqdsemu.so.1.0.6 => /home/qdsemu/qdsemu/home/lib/libqdsemu.so.1.0.6 (0xb7d03000)
libitp.so => /home/qdsemu/qdsemu/home/lib/libitp.so (0xb7cfd000)
libXi.so.6 => /usr/X11R6/lib/libXi.so.6 (0xb7cdf000)
libQt3Support.so.4 => /opt/qt-4.8.4-i386/lib/libQt3Support.so.4 (0xb79ee000)
libQtSql.so.4 => /opt/qt-4.8.4-i386/lib/libQtSql.so.4 (0xb79ad000)
libQtXml.so.4 => /opt/qt-4.8.4-i386/lib/libQtXml.so.4 (0xb7965000)
libQtNetwork.so.4 => /opt/qt-4.8.4-i386/lib/libQtNetwork.so.4 (0xb783a000)
libQtGui.so.4 => /opt/qt-4.8.4-i386/lib/libQtGui.so.4 (0xb6d17000)
libQtCore.so.4 => /opt/qt-4.8.4-i386/lib/libQtCore.so.4 (0xb6a0a000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb69f4000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb6910000)
libm.so.6 => /lib/libm.so.6 (0xb68eb000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb68e0000)
libc.so.6 => /lib/libc.so.6 (0xb67b3000)
libQtSvg.so.4 => /opt/qt-4.8.4-i386/lib/libQtSvg.so.4 (0xb675a000)
librt.so.1 => /lib/librt.so.1 (0xb6751000)
libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0xb6743000)
libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0xb664c000)
libz.so.1 => /lib/libz.so.1 (0xb6639000)
libpng12.so.0 => /usr/lib/libpng12.so.0 (0xb6615000)
libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0xb65a8000)
libSM.so.6 => /usr/X11R6/lib/libSM.so.6 (0xb659f000)
libICE.so.6 => /usr/X11R6/lib/libICE.so.6 (0xb6587000)
libXrender.so.1 => /usr/X11R6/lib/libXrender.so.1 (0xb657f000)
libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0xb6545000)
libdl.so.2 => /lib/libdl.so.2 (0xb6541000)
/lib/ld-linux.so.2 (0xb7f2d000)
libexpat.so.1 => /usr/lib/libexpat.so.1 (0xb6522000)
Re: Embedding custom fonts in Qt 4.8.4
The QFont constructor expects the family name of the font not the base name of file it came from. Typically the two do not match.
If the fonts are built in to the resources of the program then you can simply access them directly:
Code:
int id
= QFontDatabase::addApplicationFont(":/fonts/gateau.ttf");
QFont font
("chocolate cake",
13);
// << this name is the family name of the font, not the file name qDebug() << fi.family() << fi.pointSize();