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").
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:
- OS: Ubuntu 20.04
- PyQt5 source: https://anaconda.org/conda-forge/pyqt/
- PyQt5 version: 5.12.3
How can I make the text look sharper? Many thanks!