Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Embedding custom fonts in Qt 4.8.4

    General and Desktop
    3
    8
    6093
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      itpenguin last edited by

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

      @ QStringList list;
      QFontDatabase::removeAllApplicationFonts();
      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) {
      QMessageBox::warning(0, argv[0], (QString)"Impossible to open font file " + QChar(0x00AB) + *constIterator + QChar(0x00BB) + ".");
      fontWarningShown = true;
      }
      } else {
      fontID = QFontDatabase::addApplicationFontFromData(res.readAll());
      if (fontID == -1 && fontWarningShown == false) {
      QMessageBox::warning(0, argv[0], (QString)"Impossible to load " + QChar(0x00AB) + *constIterator + QChar(0x00BB) + ".");
      fontWarningShown = true;
      }
      QStringList fntList = QFontDatabase::applicationFontFamilies ( fontID );
      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;
      QString str="qdsfont1";
      QFont testFont(str,ps);
      qDebug() << FUNCIDENT << "font=" << str << "pointsize=" << ps;
      QFontInfo info(testFont);
      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
      @/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:
      @ 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)@

      1 Reply Last reply Reply Quote 0
      • jazzycamel
        jazzycamel last edited by

        If you have your TTF's in a resource why use addApplicationFontFromData() at all? Why not simply do:

        @
        QFontDatabase.addApplicationFont(':/polices/qdsFont1.ttf')
        @

        I successfully add 10+ custom fonts to my application this way.

        Hope this helps ;o)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply Reply Quote 0
        • I
          itpenguin last edited by

          Well, that makes no difference:
          @ QStringList list;
          QFontDatabase::removeAllApplicationFonts();
          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) {
          QString res(":/polices/" + *constIterator);
          fontID = QFontDatabase::addApplicationFont(res);
          if (fontID == -1 && fontWarningShown == false) {
          QMessageBox::warning(0, argv[0], (QString)"Impossible to load " + QChar(0x00AB) + *constIterator + QChar(0x00BB) + ".");
          fontWarningShown = true;
          }
          QStringList fntList = QFontDatabase::applicationFontFamilies ( fontID );
          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;
          QString str="qdsFont1";
          QFont testFont(str);
          qDebug() << FUNCIDENT << "font=" << str << "pointsize=" << ps;
          QFontInfo info(testFont);
          qDebug() << FUNCIDENT << "font=" << info.family() << "pixelsize=" << info.pointSize() << info.pixelSize() << info.weight() << info.exactMatch();
          @
          The shortened output of my program:
          @QFont= "qdsFont1.ttf" QFont= "qdsFont1"
          QFont= "qdsFont2.ttf" QFont= "qdsFont2"
          QFont= "qdsFont3.ttf" QFont= "qdsFont3"
          QFont= "qdsFont4.ttf" QFont= "qdsFont4"
          font= "qdsFont1" pointsize= 13
          font= "Arial" pixelsize= 12 12 50 false
          @

          As you can see:

          • fonts are added to the application
          • font is not used
          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Are you sure your font family name is correct ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • jazzycamel
              jazzycamel last edited by

              [quote author="SGaist" date="1371123158"]Hi,

              Are you sure your font family name is correct ?[/quote]

              This must be correct because its being returned by QFontDatabase::applicationFontFamilies() for fontID.

              Is a point size of 13 available with your font? Maybe try one of the standard sizes as returned by QFontDatabase::standardSizes(). I remember this being an issue for me as MacOSX seems to default to 13pt and I had to override it to 12pt to prevent errors with font widgets.

              Hope this helps ;o)

              For the avoidance of doubt:

              1. All my code samples (C++ or Python) are tested before posting
              2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
              1 Reply Last reply Reply Quote 0
              • I
                itpenguin last edited by

                I did not specify a point size, when creating the font ( QFont testFont(str); ).
                Nevertheless the point size of font 1 is 13.

                1 Reply Last reply Reply Quote 0
                • jazzycamel
                  jazzycamel last edited by

                  No, but you do when settings the application font.

                  Anywho, stupid question, but I take it that the fonts you are loading are proven to work elsewhere? The only reason I ask is because I have just run all the steps that you have with a number of different TTF's and I can't reproduce the problem. If the fonts are freely available somewhere I can try my tests with the actual TTF's.

                  For the avoidance of doubt:

                  1. All my code samples (C++ or Python) are tested before posting
                  2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                  1 Reply Last reply Reply Quote 0
                  • I
                    itpenguin last edited by

                    Well, actually the origin for my fonts are *.fon files (windows font format).
                    I had to translate them to ttf using fontforge.
                    I have no problem sending you one of these fontfiles.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post