Search code examples
pythonmacospyqt5qfont

Why does QFontDialog.getFont() always return the same font?


Every time I use QFontDialog.getFont() to get a font, it returns the same QFont object.

font, ok = QFontDialog.getFont()
print(font)
if ok:
    self.lbl.setFont(font)
print(self.lbl.font())

Above is the code segment, and the result is below (I make different choices but the font of the QLabel and the font I get from getFont() is always the same one).

/Users/yao/PycharmProjects/QT_test/venv/bin/python /Users/yao/PycharmProjects/QT_test/test/4_2.py
<PyQt5.QtGui.QFont object at 0x10b428748>
<PyQt5.QtGui.QFont object at 0x10b4287b8>
<PyQt5.QtGui.QFont object at 0x10b428748>
<PyQt5.QtGui.QFont object at 0x10b4287b8>

Update:

Below is some information about my environment:

  • macOS Mojave
  • PyQt5
  • Python 3.6

I have tested the codes by @ekhumoro and the results are strange:

BEFORE:
  string:.SF NS Text, 13,-1,5,50,0,0,0,0,0
  family:.SF NS Text
   size:13

AFTER:
  string:.SF NS Text, 13,-1,5,50,0,0,0,0,0
  family:.SF NS Text
   size:13

Solution

  • UPDATE:

    This seems to be caused by a bug that only affects macOS: see QTBUG-6071 and QTBUG-69878. This should have been fixed in Qt-5.12, so you need to make sure you've installed the latest versions of both Qt5 and PyQt5 in order to resolve this issue.


    Below is a test script with some screenshots of the output I get on Linux using Qt-5.12.0 with PyQt- 5.11.3. As you can see, everything works as expected. If you get different behaviour, you should edit your question and state the exact versions of Qt and PyQt you are using, which platform you are testing on, and show some sample output from the test script.

    Test Script:

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.button = QtWidgets.QPushButton('Change Font')
            self.button.clicked.connect(self.updateLabel)
            self.label = QtWidgets.QLabel()
            layout = QtWidgets.QVBoxLayout(self)
            layout.addWidget(self.label)
            layout.addWidget(self.button)
            self.updateLabel()
    
        def updateLabel(self):
            if self.label.text():
                new = QtWidgets.QFontDialog.getFont()[0]
            else:
                new = QtGui.QFont(self.label.font())
            before = self.label.font()
            self.label.setFont(new)
            after = self.label.font()
            text = []
            for font in before, after:
                info = QtGui.QFontInfo(font)
                text.append('BEFORE:' if font is before else 'AFTER:')
                text.append('  string: %s' % font.toString())
                text.append('  family: %s' % info.family())
                text.append('    size: %s' % info.pointSize())
                text.append('')
            text = '\n'.join(text)
            self.label.setText(text)
            print(text)
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setGeometry(800, 100, 500, 100)
        window.show()
        sys.exit(app.exec_())
    

    Output:

    enter image description here enter image description here enter image description here