Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows
Forum Updated to NodeBB v4.3 + New Features

How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 334 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.
  • K Offline
    K Offline
    KevinAR
    wrote on 31 Oct 2024, 04:24 last edited by
    #1

    First I used this code to set the font on my GUI.

    def set_information_font(self): # Method for 
            if self.info_item == 0:
                font = QFont("/Franklin Gothic Medium Cond", 18)  # Ganti "Arial" dan 12 sesuai keinginan
            elif self.info_item == 1:
                font = QFont("Franklin Gothic Medium Cond", 18)  # Ganti "Arial" dan 12 sesuai keinginan
                
            print(f"Setting font for info_item = {self.info_item}")
            self.ui.label_information_2.setFont(font)  # Atur font untuk label_information_2
            self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);")
            
            self.ui.label_connection.setFont(QFont("Franklin Gothic Medium Cond", 20))  # Atur font untuk label_connection
            self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
    

    So when I use this code, I use the font that has been installed in Windows.

    The GUI of the First Program output is in the below

    Pict1.jpg

    Now I want to use the font but the font source is in my file directory which is in the font folder. I tried with the code below:

    def set_information_font(self):  # Method for setting font
            font_path = os.path.join(os.path.dirname(__file__), "font/FRAMDCN.TTF")
            
            # Cek apakah font ada
            if not os.path.exists(font_path):
                print("Font tidak ditemukan:", font_path)
                return
    
            # Atur font untuk label_information_2 dan label_connection menggunakan path font
            if self.info_item == 0:
                font = QFont(font_path, 18)  # Menggunakan path langsung ke file font
            elif self.info_item == 1:
                font = QFont(font_path, 18)  # Menggunakan path langsung ke file font
            
            self.ui.label_information_2.setFont(font)  # Atur font untuk label_information_2
            self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);")
            
            self.ui.label_connection.setFont(QFont(font_path, 20))  # Atur font untuk label_connection
            self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
    

    The GUI of the Second Program output is in the below
    Pict2.jpg

    But the font is not successfully applied in the GUI. Any solution?

    Thank You

    J 1 Reply Last reply 31 Oct 2024, 06:21
    1
    • K KevinAR
      31 Oct 2024, 04:24

      First I used this code to set the font on my GUI.

      def set_information_font(self): # Method for 
              if self.info_item == 0:
                  font = QFont("/Franklin Gothic Medium Cond", 18)  # Ganti "Arial" dan 12 sesuai keinginan
              elif self.info_item == 1:
                  font = QFont("Franklin Gothic Medium Cond", 18)  # Ganti "Arial" dan 12 sesuai keinginan
                  
              print(f"Setting font for info_item = {self.info_item}")
              self.ui.label_information_2.setFont(font)  # Atur font untuk label_information_2
              self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);")
              
              self.ui.label_connection.setFont(QFont("Franklin Gothic Medium Cond", 20))  # Atur font untuk label_connection
              self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
      

      So when I use this code, I use the font that has been installed in Windows.

      The GUI of the First Program output is in the below

      Pict1.jpg

      Now I want to use the font but the font source is in my file directory which is in the font folder. I tried with the code below:

      def set_information_font(self):  # Method for setting font
              font_path = os.path.join(os.path.dirname(__file__), "font/FRAMDCN.TTF")
              
              # Cek apakah font ada
              if not os.path.exists(font_path):
                  print("Font tidak ditemukan:", font_path)
                  return
      
              # Atur font untuk label_information_2 dan label_connection menggunakan path font
              if self.info_item == 0:
                  font = QFont(font_path, 18)  # Menggunakan path langsung ke file font
              elif self.info_item == 1:
                  font = QFont(font_path, 18)  # Menggunakan path langsung ke file font
              
              self.ui.label_information_2.setFont(font)  # Atur font untuk label_information_2
              self.ui.label_information_2.setStyleSheet("color: rgb(0, 87, 151);")
              
              self.ui.label_connection.setFont(QFont(font_path, 20))  # Atur font untuk label_connection
              self.ui.label_connection.setStyleSheet("color: rgb(0, 87, 151);")
      

      The GUI of the Second Program output is in the below
      Pict2.jpg

      But the font is not successfully applied in the GUI. Any solution?

      Thank You

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 31 Oct 2024, 06:21 last edited by
      #2

      @KevinAR said in How to Use Custom Fonts from Folder in PyQt6 Instead of Installed Fonts in Windows:

      But the font is not successfully applied in the GUI.

      But both screenshots look different?
      I also do not see any QFont constructor which takes path to a font file as first parameter.
      I think you have to use https://doc.qt.io/qt-6/qfontdatabase.html#addApplicationFont

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      31 Oct 2024, 04:24

      • Login

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