qtpysidepyside2pyside6qsystemtrayicon

In PySide6, show() method for QSystemIconTrayIcon() not working after calling hide() function


My app needs to dynamically show or hide system tray. But once I call .hide() function on it, and then call .show() on it, It does not show up. Same behavior with .setVisible(...).

Minimal Reproducible Code

I've created a minimal reproducible code to show what issue is happening with QSystemTrayIcon class of PySide6.

import os
import sys
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
import resources_rc

app = QApplication(sys.argv)

# Image
def _image(name: str):
    return os.path.join(':', 'saved_files', 'images', f'{name}.png')

# Setup system tray
tray = QSystemTrayIcon(QIcon(_image('folder')))
tray.show()
print('> Running\n')

def toggle_tray():
    tray.setVisible(not tray.isVisible())
    # tray.setIcon(QIcon(_image('folder')))                             # WORKAROUND (read below)
    print(f'[State Toggled] Tray Visible = {tray.isVisible()}')

# Timer to show/hide the tray icon
timer = QTimer()
timer.timeout.connect(toggle_tray)
timer.start(3000)
    
app.exec()

How it should work:

It should show system tray icon and then after 3 seconds, hide. Then after 3 seconds, show. Then after 3 seconds, hide. And cycle continues.

How it's behaving:

It shows up perfectly on start (as it should), and then after 3 seconds it hides (as it should), but after next 3 seconds, IT DOESN'T SHOW UP (as it should have).

Workaround I found (after many trials and errors):

Info:


Solution

  • It seems it was a bug in Qt. I reported and they fixed it in v6.5.4, v6.6.1, v6.7.0.