Results 1 to 2 of 2

Thread: Embedding custom fonts in Qt 4.8.4

  1. #1
    Join Date
    Jun 2013
    Location
    Germany
    Posts
    4
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Embedding custom fonts in Qt 4.8.4

    Hello,
    we have problems embedding fonts in our application using QFontDatabase::addApplicationFontFromData:

    Qt Code:
    1. QFontDatabase::removeAllApplicationFonts();
    2. list << "qdsFont1.ttf" << "qdsFont2.ttf" << "qdsFont3.ttf" << "qdsFont4.ttf";
    3. int fontID(-1);
    4. bool fontWarningShown(false);
    5. for (QStringList::const_iterator constIterator = list.constBegin(); constIterator != list.constEnd(); ++constIterator) {
    6. QFile res(":/polices/" + *constIterator);
    7. if (res.open(QIODevice::ReadOnly) == false) {
    8. if (fontWarningShown == false) {
    9. QMessageBox::warning(0, argv[0], (QString)"Impossible to open font file " + QChar(0x00AB) + *constIterator + QChar(0x00BB) + ".");
    10. fontWarningShown = true;
    11. }
    12. } else {
    13. fontID = QFontDatabase::addApplicationFontFromData(res.readAll());
    14. if (fontID == -1 && fontWarningShown == false) {
    15. QMessageBox::warning(0, argv[0], (QString)"Impossible to load " + QChar(0x00AB) + *constIterator + QChar(0x00BB) + ".");
    16. fontWarningShown = true;
    17. }
    18. QStringList fntList = QFontDatabase::applicationFontFamilies ( fontID );
    19. for (QStringList::const_iterator constIt = fntList.constBegin(); constIt != fntList.constEnd(); ++constIt) {
    20. qDebug() << argv[0] << "QFont=" << *constIterator << "QFont=" << *constIt;
    21. }
    22. }
    23. }
    24. a.setFont(QFont("qdsFont1"),13);
    25.  
    26. #define FUNCIDENT "main"
    27. int ps=13;
    28. QString str="qdsfont1";
    29. QFont testFont(str,ps);
    30. qDebug() << FUNCIDENT << "font=" << str << "pointsize=" << ps;
    31. QFontInfo info(testFont);
    32. qDebug() << FUNCIDENT << "font=" << info.family() << "pixelsize=" << info.pointSize() << info.pixelSize() << info.weight() << info.exactMatch();
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. /opt/qt-4.8.4-src/configure --prefix=/opt/qt-4.8.4-i386 --platform=linux-g++-32 -no-glib
    2. -nomake demos -nomake examples -confirm-license -opensource -no-openssl -no-opengl -no-nis
    3. -xinput -no-cups -no-dbus -no-webkit -xrender -fontconfig
    To copy to clipboard, switch view to plain text mode 
    ldd shows me that the application uses these libs:
    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)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,620
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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:
    Qt Code:
    1. int id = QFontDatabase::addApplicationFont(":/fonts/gateau.ttf");
    2. qDebug() << id << QFontDatabase::applicationFontFamilies(id);
    3. QFont font("chocolate cake", 13); // << this name is the family name of the font, not the file name
    4. QFontInfo fi(font);
    5. qDebug() << fi.family() << fi.pointSize();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 18th June 2011, 18:28
  2. Embedding a SQLite database in a custom file type
    By sortaSean in forum Qt Programming
    Replies: 0
    Last Post: 7th December 2010, 01:35
  3. embedding X11
    By afflictedd2 in forum Qt Programming
    Replies: 5
    Last Post: 13th February 2009, 08:38
  4. Embedding a custom DLL on Mac
    By sdfisher in forum Installation and Deployment
    Replies: 4
    Last Post: 27th March 2007, 18:48
  5. Embedding
    By shrikarcse in forum Newbie
    Replies: 1
    Last Post: 27th March 2006, 19:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.