Skip to content

yjg30737/pyqt-font-dialog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyqt-font-dialog

PyQt "select the font" dialog and widget

Requirements

PyQt5 >= 5.8

Setup

python -m pip install pyqt-font-dialog

Class/Method Overview

  • FontDialog(font: QFont = QFont('Arial', 10), title='Font') - font is font, title is title of the dialog.
    • getFont() - get the selected font.
    • getFontWidget() - get the main FontWidget of the dialog.
  • FontWidget(font: QFont = QFont('Arial', 10))
    • fontChanged(QFont) - When current font item is changed, this will be emitted.
    • getFont() - get the selected font.
    • setCurrentFont(QFont) - set the current font.

Usage

1. As a dialog

dialog = FontDialog(textEdit.currentFont())
reply = dialog.exec()
if reply == QDialog.Accepted:
    textEdit.setCurrentFont(dialog.getFont())

Preview

image

2. As a part of window

from PyQt5.QtWidgets import QMainWindow, QApplication, QHBoxLayout, QWidget, QTextEdit
from pyqt_font_dialog.fontWidget import FontWidget


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.__initUi()

    def __initUi(self):
        self.__te = QTextEdit()
        fontWidget = FontWidget()
        fontWidget.fontChanged.connect(self.fontChanged)
        lay = QHBoxLayout()
        lay.addWidget(self.__te)
        lay.addWidget(fontWidget)
        mainWidget = QWidget()
        mainWidget.setLayout(lay)
        self.setCentralWidget(mainWidget)

    def fontChanged(self, font):
        self.__te.setFont(font)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(app.exec_())

Preview

pyqt_font_dialog_example.mp4