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. Custom font for whole application

Custom font for whole application

Scheduled Pinned Locked Moved Solved General and Desktop
fontfont familycustom fontqrc
3 Posts 2 Posters 2.0k 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.
  • D Offline
    D Offline
    DeanLee
    wrote on 25 Jul 2022, 08:39 last edited by DeanLee
    #1

    Hi, I want to apply custom font to whole application.
    So, I imported font files(.otf) into .qrc

    <RCC>
        <qresource prefix="/fonts">
            <file>res/SCDream1.otf</file>
            <file>res/SCDream2.otf</file>
            <file>res/SCDream3.otf</file>
            <file>res/SCDream4.otf</file>
            <file>res/SCDream5.otf</file>
            <file>res/SCDream6.otf</file>
            <file>res/SCDream7.otf</file>
            <file>res/SCDream8.otf</file>
            <file>res/SCDream9.otf</file>
        </qresource>
    </RCC>
    

    And call QFontDatabase::addApplicationFont() in main

    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	
    	// load font
    	int n = -1;
    	for (int i = 1; i < 10; ++i) {
    		n = QFontDatabase::addApplicationFont(QString(":/fonts/res/SCDream%1.otf").arg(i));  // (1)
    		qDebug() << QFontDatabase::applicationFontFamilies(n);  // (2)
    	}
    
    	// set application's default font
    	QFont defaultFont;
    	defaultFont.setFamily("S-Core Dream");
    	defaultFont.setPointSize(18);
    	a.setFont(defaultFont);
    	qDebug() << a.font().family();  // (3)
    	
    	// ...
    	myApp w;
    	w.show();
    	return a.exec();
    }
    

    In (1), return value of addApplicationFont() is 0 to 8. it's fine.
    In (2), debug message is

    ("S-Core Dream 1 Thin")
    ("S-Core Dream 2 ExtraLight")
    ("S-Core Dream 3 Light")
    ("S-Core Dream 4 Regular")
    ("S-Core Dream 5 Medium")
    ("S-Core Dream 6 Bold")
    ("S-Core Dream 7 ExtraBold")
    ("S-Core Dream 8 Heavy")
    ("S-Core Dream 9 Black")
    

    and this is also fine.
    In (3), debug message for the font set in the application is

    "S-Core Dream"
    

    and that's fine too. (maybe not?)

    But application fonts are not changed at all.
    In each UI, except for the type of font, the size and weight are set using a setStyleSheet like :

    // child widget's constructor
    setStyleSheet("pushButton {font-size: 50px; font-weight: 600;}");  
    // The font-size is applied, but the weight is not applied. I think it's because it's a default font (which doesn't support weights), not a custom font type.
    

    At first, there was no problem in my PC, but when I ran it on another Windows 10 PC for testing, the font was applied or not depending on the PC, and it seems to be affected by whether the font is installed on the PC, but I do not understand . The font was definitely included in the .qrc and built (size of exe is increases), and the debugging message appeared the same (all load and set is fine) even on the PC where the font was not applied.

    I tried calling by specifying the path without adding each font file to .qrc, but it didn't work.

    QFontDatabase::addApplicationFont(QString(a.applicationDirPath() + "/fonts/SCDream%1.otf").arg(i)); 
    

    What am I missing?

    I am developing with Qt Visual Studio addOn(Qt VS Tools) with VS2017 on Windows 10 21H2, Qt version is 5.14.1.

    1 Reply Last reply
    1
    • C Offline
      C Offline
      ChrisW67
      wrote on 2 Aug 2022, 02:29 last edited by
      #2

      Each of your font files provides an independent font family, as shown in your debug output. Note that none of these families is "S-Core Dream", so a substitution may be occurring. From the docs, " If the family isn't available a family will be set using the font matching algorithm."

      You could pass defaultFont to QFontInfo to see what is actually selected.
      What happens if you set defaultFont with family "S-Core Dream 4 Regular"?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DeanLee
        wrote on 17 Nov 2022, 17:03 last edited by
        #3

        So, after long time, finally I can fix it.
        I don't know why, but *.ttf font file is works fine.
        Qt says support both otf and ttf file, but it's not.

        1 Reply Last reply
        0

        • Login

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