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(...)
.
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()
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.
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).
tray.setIcon(QIcon(_image('folder')))
after hiding the tray icon using .hide()
or .setVisible(False)
..hide()
or .setVisible(False)
is removing the icon from QSystemTrayIcon()
or is it something else? Maybe It's a Bug? I really need your help regarding this.It seems it was a bug in Qt. I reported and they fixed it in v6.5.4, v6.6.1, v6.7.0.