Solved I write a class TrayIcon inherit QSystemTrayIcon, why I can't call show() directly? TrayIcon().show()
-
@JonB
Maybe this is a feature of pyqt. Thanks for your reply.from PySide2.QtWidgets import QErrorMessage, QApplication app = QApplication([]) error_dialog = QErrorMessage() error_dialog.showMessage('Oh no!') QErrorMessage().showMessage('Oh no!') app.exec_()
class Test: def __init__(self): self.x = 1 def print(self): print(self.x) Test().print()
-
from PySide2.QtWidgets import QApplication, QMessageBox app = QApplication([]) QMessageBox().critical(None, 'Error', "A Error A Error A Error", buttons=QMessageBox.Ok) app.exec_()
-
Hi,
Can you show the exact code you are using with your custom tray icon class ?
@JonB is more than likely correct with the object getting garbage collected.
-
@ideaplus
Not sure what you are proving with yourQMessageBox()
example, the original wasQSystemTrayIcon
,QMessageBox()
may work differently. In any case, yourQErrorMessage()
example is assigned to a variable whose scope persists, and yourQMessageBox().critical()
blocks until the user dismisses it, so in both cases they will not be collected to stop it working, and I would expect to work under PyQt as well as PySide2.I can't comment on PySide2 behaviour, may differ from PyQt in this respect. I do know that in PyQt I came a similar cropper when I created a modeless
QDialog
which had no parent (i.e. application-modeless). I found that if I did not keep a global reference to the modeless dialog --- even though I never actually used it anywhere --- then I simply would not get to see it, as it was being deleted/garbage collected immediately after creation! -
-
And as @JonB answered, in the second version, there's nothing holding any reference to the underlying object hence it gets garbage collected and destroyed before being even shown.
-
@Denni-0 said in I write a class TrayIcon inherit QSystemTrayIcon, why I can't call show() directly? TrayIcon().show():
And actually in neither of them is the code actually holding onto a reference to the underlying objects as self. is not used in either case
That's not how referencing works in Python, or at least not as it applies to this case.
def main(): # It works: my_tray_icon = TrayIcon(my_widget) my_tray_icon.show() sys.exit(app.exec_())
my_tray_icon
is only a local variable to a function, so nothing to do withself
. However, its scope extends to the end of the function. Even though it is not referenced again after the.show()
, its reference persists until the function exits, and will not be garbage collected until then. Since it is still in scope across theapp.exec_()
, that is why it works. -
@Denni-0
Thanks for your comment.I just wanted the OP to know that what you wrote about "in neither of them is the code actually holding onto a reference to the underlying objects as
self.
is not used in either case" is incorrect, in case he cares even if you do not.I take your comments about the lack of clarity/confusion in my posts compared to yours, and will try to learn.
All the best for New Year :)
-
@Denni-0
I persist because you persist :)
Let's move on now, as I said I wish you all the best for 2020! -
@Denni-0
It would probably depend on how much I couldn't live with what you posted, but on this occasion/thread I will try to be good and just say nothing ;-)