0

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):

enter image description here

Strangely, when I run the app via system terminal (bash), icon size is not respected, and the text is shrunk (see below):

enter image description here

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()
Blademaster
  • 324
  • 2
  • 15
  • 1
    Are you using the same python in both cases? Most likely, when using different versions you probably have different versions of pyqt, or compiled differently (for example anaconda compiles with its own binaries, the ubuntu repository also compiles it with its own binaries and Qt with others so they could vary) So please give more information. – eyllanesc Oct 06 '21 at 02:21
  • 1
    Also, if you have anything to say about the post closure, use comments: reviewers cannot directly see what has been changed from the original post and they usually rely on comments to know about changes, otherwise they (*we*) might not realize if the edit was done *before* or *after* the closure. Please consider that we get thousands of posts each day, we cannot always review them as carefully as we would like to: comments are a way to let people know that something has *actually* changed in the post without requiring us to go to the post history to understand what has been changed. – musicamante Oct 06 '21 at 02:27
  • @eyllanesc Yes, I'm using the same virtual environment in vscode and system terminal. The PyQt5 used in the virtual env comes from conda-forge: https://anaconda.org/conda-forge/pyqt/ – Blademaster Oct 06 '21 at 11:09

1 Answers1

0

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!

Blademaster
  • 324
  • 2
  • 15