Issue
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):
- Workaround: Set the icon for system tray again using
tray.setIcon(QIcon(_image('folder')))
after hiding the tray icon using.hide()
or.setVisible(False)
. - Result: Everything works as intended, but that's just a workaround.
- Question: I don't understand why the original code is not working? Is it because
.hide()
or.setVisible(False)
is removing the icon fromQSystemTrayIcon()
or is it something else? Maybe It's a Bug? I really need your help regarding this.
Info:
- Platform: Windows 11 Pro v22621.2361 (stable build)
- Python version: Python 3.11.4
- PySide version: PySide6==6.5.3 | PySide6-Addons==6.5.3 | PySide6-Essentials==6.5.3
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.
Answered By - Ecto Ruseff
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.