2

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
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Yao
  • 53
  • 7
  • 1
    It gives you a `QFont` object at the same location in memory.. whats the result of `print(self.lbl.font().toString())`? – Jonas Feb 02 '19 at 12:52
  • A `QFont` is used for ***requesting*** a font from the system. There is never any guarantee that you will get exactly what you asked for. You should always use `QFontInfo` to get information about the actual font that has been provided by the system. – ekhumoro Feb 02 '19 at 17:32
  • Well, I have used the method toString() to get the description of the font, and it yields the same result: .SF NS Text,13,-1,5,50,0,0,0,0,0 – Yao Feb 04 '19 at 13:26

2 Answers2

1

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

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • I have tested the codes you posted, and I have updated this question with the result. I guess the reason that I get the same output may be my OS, but I'm not sure because I don't get relative information about this. – Yao Feb 06 '19 at 10:34
  • 1
    @Yao See my updated answer. There seems to be a bug that only affects macOS. If you update your qt and pyqt installation to the latest versions, it should be fixed. (PS: if you found this answer useful, please mark it as accepted by clicking the tick symbol above). – ekhumoro Feb 06 '19 at 16:10
  • Thanks for your help. I have checked versions of my QT and PyQt. I'm using PyQt(5.11.3) and QT(5.9.7). However, my QT is bundled with Anaconda, so I'm afraid that something there won't work properly if I update my QT. So I can't check this answer, but now I do know that it's an uncommon problem with macOS. Again, thanks for your test codes and issues about this bug, and I have marked this answer as accepted. :) – Yao Feb 07 '19 at 14:00
0

<PyQt5.QtGui.QFont object at 0x10b4287b8> only gives you the location in memory of your object, it's basically a memory cell number, but in hexadecimal. You can have a different object (here, a different QFont object), stored in the same memory space.

You could check that the QFont object here is the one you selected by doing this:

font, ok = QFontDialog.getFont()
print(font.toString())

Update: Could you try the following code and tell us what you get?

import sys
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QLabel, QApplication, QDialog, QVBoxLayout

app = QApplication(sys.argv)

dlg = QDialog()
layout = QVBoxLayout()

for fontname in ["Verdana", "Arial", "Consolas"]:
    widget = QLabel(fontname)
    font = QFont(fontname)
    widget.setFont(font)
    layout.addWidget(widget)

dlg.setLayout(layout)
dlg.show()

r = app.exec_()
olinox14
  • 6,177
  • 2
  • 22
  • 39
  • .SF NS Text,13,-1,5,50,0,0,0,0,0 I have used the method toString(), and I get the same output before the if segment and after that. – Yao Feb 04 '19 at 13:32
  • @Yao Why do you think it should be different? And why does it matter? Are you saying that the label does not render the font correctly? – ekhumoro Feb 04 '19 at 18:50
  • @ekhumoro Yes. The setFont() method seems to be invalid, and the appearance of this label doesn't change at all. – Yao Feb 05 '19 at 08:21
  • I added a working example of use of various fonts, could you try it? – olinox14 Feb 05 '19 at 08:50
  • @Yao It all works exactly as expected for me. Please try the test script in my answer. – ekhumoro Feb 05 '19 at 18:03
  • @olinox14 Thanks for your test codes. I have tested the codes. I don't know how to describe my window, but the three labels within the window seem to be same...I also tested codes by ekhumoro, and I have update this question and include description about the appearance. – Yao Feb 06 '19 at 10:47