QPainter.drawText output is blurry

437 views Asked by At

I'm trying to create a system tray icon to display a string ("Hello world!" in the code snippet below) using QPainter's drawText. The problem is that the text is not crisp, and is rather blurry (I have a HiDPI monitor if that's relevant). Here's a screenshot of my system tray (compare the date/time with "Hello world").

enter image description here

And here's the code that generates the system tray icon.

AR = 2
height = 256

pixmap = QtGui.QPixmap(height*AR, height)
pixmap.fill(QtCore.Qt.transparent)

font = QtGui.QFont()
font.setPixelSize(height/2)
font.setStretch(int(100/AR))  # %, otherwise label will be stretched

painter = QtGui.QPainter(pixmap)
painter.setFont(font)
painter.setPen(QtGui.QColor("white"))
painter.drawText(pixmap.rect(), QtCore.Qt.AlignVCenter, "Hello world!")
painter.end()

icon = QtGui.QIcon()
icon.addPixmap(pixmap)

Edit 1: Once created, the icon is used in the following context:

QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)

app = QApplication(sys.argv)
trayIcon = QSystemTrayIcon(icon)
trayIcon.show()
sys.exit(app.exec_())

Edit 2: Here are some additional info:

How can I make the text look sharper? Many thanks!

0

There are 0 answers