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

Using custom fonts

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 30.5k Views
  • 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.
  • M Offline
    M Offline
    MichiS97
    wrote on last edited by
    #3

    I've put this into my MainWindow class now:

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

    However, my application doesn't even start now. It compiles without any errors but if I try to run it Qt Creator says "The program has unexpectedly finished." and Windows gives me its default "Programm has unfortunately stopped" message.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #4

      @MichiS97 said:
      Hi and welcome

      Have you checked what addApplicationFont returns ?

      Also, a other user seemed to make it work by dynamic allocation instead of auto
      when testing the font. Please see.
      https://forum.qt.io/topic/23875/how-to-use-qfontdatabase-addapplicationfont

      And last note, the path for the ttf file
      have you tried
      QFontDatabase::addApplicationFont("resources/Triforce.ttf"))
      (no qrc)

      Also if you right click the ttf in the project tree and select "Copy path" , and try with that one.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MichiS97
        wrote on last edited by MichiS97
        #5

        Thanks for your reply.

        Using the qrc URL addApplicationFont returns -1 (which it shouldn't, I guess) as well as when I use the path "resources/Triforce.ttf". When I copy the path from the project tree addApplicationFont returns 0 but the application still crashes right away.

        I'm going to look at the dynamic method, too, thanks :)

        EDIT: Tried it, same result >.<

        1 Reply Last reply
        0
        • David.GD Offline
          David.GD Offline
          David.G
          wrote on last edited by David.G
          #6

          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]");
          
          David.GD 1 Reply Last reply
          0
          • David.GD David.G

            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]");
            
            David.GD Offline
            David.GD Offline
            David.G
            wrote on last edited by
            #7

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichiS97
              wrote on 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
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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
                0
                • mrjjM mrjj

                  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 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?

                  mrjjM 1 Reply Last reply
                  0
                  • David.GD Offline
                    David.GD Offline
                    David.G
                    wrote on 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 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

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • mrjjM mrjj

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

                          M Offline
                          M Offline
                          MichiS97
                          wrote on last edited by
                          #14

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

                          mrjjM 1 Reply Last reply
                          0
                          • M MichiS97

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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 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
                            0
                            • mrjjM mrjj

                              @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 last edited by
                              #16

                              @mrjj Yup, works like a charm

                              mrjjM 1 Reply Last reply
                              0
                              • M MichiS97

                                @mrjj Yup, works like a charm

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 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
                                • David.GD Offline
                                  David.GD Offline
                                  David.G
                                  wrote on 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
                                  1
                                  • David.GD David.G

                                    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 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!!

                                    mrjjM 1 Reply Last reply
                                    0
                                    • M MichiS97

                                      @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!!

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @MichiS97
                                      Super. Good work.

                                      1 Reply Last reply
                                      1

                                      • Login

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