2

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!

Blademaster
  • 324
  • 2
  • 15
  • 1
    Try adding `QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)`. Also you should `end()` the painter before setting the pixmap on the icon. – musicamante Oct 05 '21 at 10:59
  • @musicamante Thanks for your comment. I already had it in my header (forgot to include it in the question). Just updated the question. The screenshot shows the output when I have it enabled. If I disable it, it's much blurrier (to the point that it's barely legible). So, it helps, but still it's not as sharp as it should be (on a HiDPI monitor). – Blademaster Oct 05 '21 at 12:30
  • 1
    @Blademaster I just realized that there's a specific section in the documentation about [High DPI Icons](https://doc.qt.io/qt-5/qicon.html#high-dpi-icons). Unfortunately, I don't own a HiDPI setup, so I can't test it and provide a valid answer, and it's also a bit problematic as it requires physically saving the file (which might not be optimal). A possible solution would be to subclass QIconEngine, override `paint()` and do the painting based on the painting device pixel ratio. – musicamante Oct 05 '21 at 13:10
  • @musicamante Thanks for the link. I tried saving the file, and adding it vs `addFile`, but it didn't help. I just realized that the blurriness comes from the non-unit aspect ratio. If I instantiate `QPixmap` as a square (i.e., `QPixmap(heigh, height)` instead of `QPixmap(width, height)`), the text looks sharp, although it's truncated as it doesn't fit in the square bounding box. Any idea how to circumvent that? – Blademaster Oct 05 '21 at 14:35
  • 1
    @Blademaster uhm, now that you mention that, I remember that I did some research on QSystemTrayIcon with non square icons, and found almost anything; being on Linux, I only realized that non square icons are not considered standard for tray icons, so in a program of mine I used a workaround: multiple icons placed one aside the other. It's not very elegant, but it's effective. Unfortunately I'm not sure if Windows actually uses a spacing between icons, if that's the case, I really don't know what you could do. – musicamante Oct 05 '21 at 14:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237846/discussion-between-blademaster-and-musicamante). – Blademaster Oct 05 '21 at 18:26

0 Answers0