PyQt - QFontComboBox Widget



The QFontComboBox widget is combobox that is used to select font family in PyQt applications. It gives the user to choose from an alphabetically sorted list of font family names. Some font family names include Arial, Helvetica, and Times New Roman etc.

Inheritance

QFontComboBox class inherits its core functionality from QComboBox class which extracts its properties from QWidgets class.

pyqt qfontcombobox inheritance

Key features of QFontComboBox

  • Font Family Selection − Using font family selection users can easily select font families from an alphabetized list presented within the combobox.
  • Visual Representation − Font are displayed using their actual styles whenever possible which helps users in visually identifying their preferred font.
  • Customization − We can filter fonts based on certain criteria like scalability and writing system.
  • Integration − It is commonly integrated into toolbars alongside other font-related controls like font size selectors and style toggles.

Example 1: Basic QFontComboBox Implementation

In this example, a simple PyQt application is created with a single QFontComboBox widget added to the main window. Upon running the application, users can interactively select font families from the combobox.

Here, the initUI method initializes the user interface by setting up a QVBoxLayout to arrange widgets vertically, adding the QFontComboBox to the layout, and setting the window's title to 'Font Selector'. Finally, the application is executed using QApplication, displaying the window and handling events until the application exits.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox

class FontSelector(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      layout = QVBoxLayout()
      font_combo = QFontComboBox()
      layout.addWidget(font_combo)
      self.setLayout(layout)
      self.setWindowTitle('Font Selector')

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = FontSelector()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following result −

pyqt qfontcombobox example 1

Example 2: Filtering fonts with QFontComboBox

In the below example we will create a font selector which will show only the scalable fonts. When we select a font from the combobox, the label below it will update to show the selected font family.

In a FontSelector QWidget, a QVBoxLayout arranges widgets. A QFontComboBox, font_combo, displays only scalable fonts due to setFontFilters(). A QLabel, font_label, updates to show the selected font family via update_label() when the font selection changes.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QFontComboBox, QLabel

class FontSelector(QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()

   def initUI(self):
      layout = QVBoxLayout()

      # Create QFontComboBox
      font_combo = QFontComboBox()

      # Set font filters to show only scalable fonts
      font_combo.setFontFilters(QFontComboBox.FontFilter.ScalableFonts)

      # Create a label to display the selected font
      self.font_label = QLabel("Selected Font")

      # Connect fontChanged signal to update_label slot
      font_combo.currentFontChanged.connect(self.update_label)

      layout.addWidget(font_combo)
      layout.addWidget(self.font_label)
      self.setLayout(layout)
      self.setWindowTitle('Font Selector')

   def update_label(self, font):
      # Update the label text with the selected font family
      self.font_label.setText(f"Selected Font: {font.family()}")

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = FontSelector()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following result −

pyqt qfontcombobox example 2
Advertisements