Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using custom fonts
Forum Updated to NodeBB v4.3 + New Features

Using custom fonts

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 31.0k Views 2 Watching
  • 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.
  • D David.G
    16 Aug 2015, 18:41

    Could you check if the file exists within the QRC?

    #include <QFile>
    #include <QDebug>
    ....
        QFile file(":/resources/Triforce.ttf");
        qDebug() << file.exists();
        QFile file(":/Triforce.ttf");
        qDebug() << file.exists();
    

    If none returns then there's a problem with the resource file.

    If one of them returns true then

    QFontDatabase::addApplicationFont("[string of the one that returned true]");
    
    D Offline
    D Offline
    David.G
    wrote on 16 Aug 2015, 18:44 last edited by
    #7

    Woops, edited the code. forgot to call exists()

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MichiS97
      wrote on 16 Aug 2015, 18:51 last edited by MichiS97
      #8

      Damn....neither of the two exist....what could I have done wrong with the resource file?

      EDIT: Got it!! Seems like you shouldn't choose the same name for the folder in which the ttf is as the prefix for your qrc file. Now qDebug returns true but it sill won't work...

      EDIT2: I just don't understand anything anymore. Like I said in the first edit, qDebug returned true for both paths. Well....it did...once. I proceeded to use Clean All and QMake afterwards and now it doesn't work anymore.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 16 Aug 2015, 19:02 last edited by
        #9

        Hi I think it maybe be related to the rc file somehow.
        I added a qrc to a project and then directly added the ttf file.
        and then in mainwindow constructor

        int loadedFontID = QFontDatabase::addApplicationFont ( ":/Triforce.ttf" );
        QFont Triforce("Triforce", 24, QFont::Normal);
        ui->label->setFont(Triforce);
        

        and it just worked. ?! So I think you are really close to nailing it :)

        http://postimg.org/image/yy8plt7ax/

        M 1 Reply Last reply 16 Aug 2015, 19:07
        0
        • M mrjj
          16 Aug 2015, 19:02

          Hi I think it maybe be related to the rc file somehow.
          I added a qrc to a project and then directly added the ttf file.
          and then in mainwindow constructor

          int loadedFontID = QFontDatabase::addApplicationFont ( ":/Triforce.ttf" );
          QFont Triforce("Triforce", 24, QFont::Normal);
          ui->label->setFont(Triforce);
          

          and it just worked. ?! So I think you are really close to nailing it :)

          http://postimg.org/image/yy8plt7ax/

          M Offline
          M Offline
          MichiS97
          wrote on 16 Aug 2015, 19:07 last edited by
          #10

          @mrjj
          That doesn't work either....
          where did you put the ttf file? In the same folder as the cpp files?

          M 1 Reply Last reply 16 Aug 2015, 19:14
          0
          • D Offline
            D Offline
            David.G
            wrote on 16 Aug 2015, 19:08 last edited by
            #11

            Been wondering, are you using linux? windows? osx? If linux you need to have fontconfig installed (according to documentation)

            I'm sort of running out of suggestions, hopefully someone senior appears as like you I've recently started on this journey with C++/Qt .

            I guess my last suggestion would be instead of calling addapplicationfont call

             QFile file(":/Triforce.ttf"); // the one that returned true
            qDebug () << QFontDatabase::addApplicationFontFromData(file.readAll());
            
            

            As for the implications of loading it like thatI wouldn't know. Best of luck.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichiS97
              wrote on 16 Aug 2015, 19:13 last edited by
              #12

              I'm using Windows 10...
              Calling addApplicationFontFromData gives me this in the application output:
              QIODevice::read (QFile, ":\Triforce.ttf"): device not open
              QFSFileEngine::open: No file name specified
              -1

              1 Reply Last reply
              0
              • M MichiS97
                16 Aug 2015, 19:07

                @mrjj
                That doesn't work either....
                where did you put the ttf file? In the same folder as the cpp files?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 16 Aug 2015, 19:14 last edited by
                #13

                @MichiS97
                Yes ttf in same folder as cpp files. Then right clicked qrc and "add existing file" .

                M 1 Reply Last reply 16 Aug 2015, 19:17
                0
                • M mrjj
                  16 Aug 2015, 19:14

                  @MichiS97
                  Yes ttf in same folder as cpp files. Then right clicked qrc and "add existing file" .

                  M Offline
                  M Offline
                  MichiS97
                  wrote on 16 Aug 2015, 19:17 last edited by
                  #14

                  @mrjj I did it in the exact same way, something seems to hate my guts :D

                  M 1 Reply Last reply 16 Aug 2015, 19:20
                  0
                  • M MichiS97
                    16 Aug 2015, 19:17

                    @mrjj I did it in the exact same way, something seems to hate my guts :D

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 16 Aug 2015, 19:20 last edited by
                    #15

                    @MichiS97
                    Could you try this mini test ?
                    https://www.dropbox.com/s/h75uamsxonvvczx/myhappyfont.zip?dl=0

                    Works here on win 7.

                    M 1 Reply Last reply 16 Aug 2015, 19:22
                    0
                    • M mrjj
                      16 Aug 2015, 19:20

                      @MichiS97
                      Could you try this mini test ?
                      https://www.dropbox.com/s/h75uamsxonvvczx/myhappyfont.zip?dl=0

                      Works here on win 7.

                      M Offline
                      M Offline
                      MichiS97
                      wrote on 16 Aug 2015, 19:22 last edited by
                      #16

                      @mrjj Yup, works like a charm

                      M 1 Reply Last reply 16 Aug 2015, 19:35
                      0
                      • M MichiS97
                        16 Aug 2015, 19:22

                        @mrjj Yup, works like a charm

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 16 Aug 2015, 19:35 last edited by
                        #17

                        @MichiS97

                        Hehe I buy your guts theory.. :)

                        Ok, so my best bet is something with the rc file.

                        could you try with the one from the test ?
                        remember to put .ttf in same folder

                        Just let Creator compile it.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          David.G
                          wrote on 16 Aug 2015, 19:44 last edited by
                          #18

                          Exactly what mrjj says, QtCreator handles resources just fine there's no need to compile it manually. You can add your qrc to your .pro file and avoid all those headaches. Moreover, you can edit/add files and prefixes as you see fit through QtCreator.

                          I tested the app since I got curious. http://i.imgur.com/mhtj26i.png

                          One thing to keep in mind. If you add text under Rich Text, QT will respect the HTML generated and it will ignore any attempts to manipulate it. Remove the HTML in the label if there's any, else it will never render with the selected font.

                          M 1 Reply Last reply 16 Aug 2015, 19:53
                          1
                          • D David.G
                            16 Aug 2015, 19:44

                            Exactly what mrjj says, QtCreator handles resources just fine there's no need to compile it manually. You can add your qrc to your .pro file and avoid all those headaches. Moreover, you can edit/add files and prefixes as you see fit through QtCreator.

                            I tested the app since I got curious. http://i.imgur.com/mhtj26i.png

                            One thing to keep in mind. If you add text under Rich Text, QT will respect the HTML generated and it will ignore any attempts to manipulate it. Remove the HTML in the label if there's any, else it will never render with the selected font.

                            M Offline
                            M Offline
                            MichiS97
                            wrote on 16 Aug 2015, 19:53 last edited by MichiS97
                            #19

                            @David.G I did everything qrc related with the Qt Creator...
                            The only thing I compiled myself was the rcc file since that was mentioned in the wiki. Seeing as how mrjj's app works fine without it I've deleted the rcc though.

                            @mrjj I tried it with your qrc as well (even though I knew that they were exactly the same) and I still get the same error

                            Update: heh....I got it to work...seems to be a bad idea to do the font stuff before the setupUi line....seems obvious enough once you see it. I've already tried it in my VM and yes, it really works now! Thanks for all your help!!

                            M 1 Reply Last reply 16 Aug 2015, 20:01
                            0
                            • M MichiS97
                              16 Aug 2015, 19:53

                              @David.G I did everything qrc related with the Qt Creator...
                              The only thing I compiled myself was the rcc file since that was mentioned in the wiki. Seeing as how mrjj's app works fine without it I've deleted the rcc though.

                              @mrjj I tried it with your qrc as well (even though I knew that they were exactly the same) and I still get the same error

                              Update: heh....I got it to work...seems to be a bad idea to do the font stuff before the setupUi line....seems obvious enough once you see it. I've already tried it in my VM and yes, it really works now! Thanks for all your help!!

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 16 Aug 2015, 20:01 last edited by
                              #20

                              @MichiS97
                              Super. Good work.

                              1 Reply Last reply
                              1

                              16/20

                              16 Aug 2015, 19:22

                              • Login

                              • Login or register to search.
                              16 out of 20
                              • First post
                                16/20
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved