Issue
I have the following simple Qt app that creates a system tray icon from a given text. When I run the app via vscode terminal, everything seems to be fine (see the screenshot below):
Strangely, when I run the app via system terminal (bash), icon size is not respected, and the text is shrunk (see below):
I'd appreciate it if anyone could shed some light what might be causing this strange behaviour. Here's the code:
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtWidgets, QtGui, QtSvg
def create_tray_icon(label):
r"""Creates QIcon with the given label."""
w, h = 22*4, 22
pixmap = QtGui.QPixmap(w, h)
pixmap.fill(QtCore.Qt.transparent) # alternative: QtGui.QColor("white")
painter = QtGui.QPainter(pixmap)
painter.setPen(QtGui.QColor("white"))
align = int(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
painter.drawText(pixmap.rect(), align, str(label))
painter.end()
icon = QtGui.QIcon()
icon.addPixmap(pixmap)
return icon
class SystemTrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtWidgets.QMenu(parent)
exitAction = self.menu.addAction("Exit")
exitAction.triggered.connect(lambda: sys.exit())
self.setContextMenu(self.menu)
def memprofilerApp():
r"""Runs the Qt Application."""
QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)
app = QApplication(sys.argv)
icon = create_tray_icon(label="Hello world!")
trayIcon = SystemTrayIcon(icon)
trayIcon.show()
sys.exit(app.exec_())
if __name__ == '__main__':
memprofilerApp()
Solution
After inspecting environment variables (by running env
) on vscode terminal and comparing it with that on system terminal, it turned out that the culprit was XDG_CURRENT_DESKTOP
! In vscode terminal (which QIcon size is respected), it is set to UNITY
, whereas in system terminal it is set to ubuntu:GNOME
.
I don't know the root cause, but a quick fix is to use the following script to run the app:
#!/bin/bash
XDG_CURRENT_DESKTOP=Unity
/path/to/python main.py
Just out of curiosity, if anyone knows why ubuntu:GNOME
cause the QIcon sizing to break, please let us know!
Answered By - Blademaster
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.