How to pass QSystemTrayIcon control between QDialog and QmainWindow
-
i have QDialog that closing after its done its flow and i start QmainWinodw , in the QDialog i init QSystemTrayIcon with the QDialog as parent
after the Qdialog closed , i like to change the QSystemTrayIcon icon from the QmainWinodw , how can i pass the control of the QSystemTrayIcon from QDialog to QMainWindow ? -
Well, the most obvious way would be to not make the QDialog the parent of your QSystemTrayIcon, but QApplication. That object stays alive for the whole time, so that would be the obvious candidate. Other than that, you could aways call ::setParent() on the QSystemTrayIcon with a pointer to your new QMainWindow.
-
Sure, that would work, but it would cause unneeded flicker in the system tray and generally waste resources. I would just make a single object controlling your QSystemTrayIcon, and create a single instance of that object with the QApplication as its parent. Much easier and cleaner.
-
"tone"? What is that?
I mean to make a single class that is responsible for handling the icon. From where you instantiate that class, is not very relevant. It may be from the main, it may be from the QDialog, or it could even be from a subclassed QApplication. You could even considder making that class a singleton class, so it would instantiated by itself the first time it is accessed. The parent of that class would not be a class that is not around for the whole time you need the icon. That rules out both QDialog and QMainWindow. QApplication would be a logical candidate. In either case, note that the question of who instantiates the object is unrelated to the question of what object will be parent. QDialog can instantiate the object, and still make the qApp the parent.
-
QApplication does not inherit from QWidget. In inherits from QObject (just like QWidget does). Furtunately, QSystemTrayIcon takes a QObject* as the parent argument. So why do you need to try to cast QApplication to a QWidget? That will not work, and is not needed.