当来自系统终端 运行 而不是 vscode 时,QSystemTrayIcon 大小不受重视

QSystemTrayIcon size not respected when run from system terminal but not vscode

我有以下简单的 Qt 应用程序,它根据给定的文本创建系统托盘图标。当我通过 vscode 终端 运行 应用程序时,一切似乎都很好(见下面的屏幕截图):

奇怪的是,当我通过系统终端(bash)运行 应用程序时,图标大小不受尊重,文本为 sh运行k(见下文):

如果有人能阐明可能导致这种奇怪行为的原因,我将不胜感激。这是代码:

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

在 vscode 终端上检查环境变量(通过 运行ning env)并与系统终端上的环境变量进行比较后,发现罪魁祸首是 XDG_CURRENT_DESKTOP!在 vscode 终端(尊重 QIcon 大小)中,它被设置为 UNITY,而在系统终端中它被设置为 ubuntu:GNOME.

我不知道根本原因,但快速解决方法是对 运行 应用程序使用以下脚本:

#!/bin/bash
XDG_CURRENT_DESKTOP=Unity
/path/to/python main.py

出于好奇,如果有人知道 ubuntu:GNOME 导致 QIcon 大小调整中断的原因,请告诉我们!