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.

  • is it ok to delete the QSystemTrayIcon when the QDialog is closed ( destroyed )
    and then i set new QSystemTrayIcon in the QMainWindow . works ok dont know if its the best way to do this

  • 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.

  • you mean to make single tone ? in the main ? not related not to the QDialog and the QMainWindow?

  • "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.

  • ok thanks i got you now

  • i tried to put the QApplication as the parent of the QSystemTrayIcon and i saw it inherited fro QWidget
    but it gives me error it can't cast from from QApplication to QWidget .

  • 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.

  • my mistake , thanks

  • hi again , i have another problem here .
    what to do with the new QMenu that is set with the ContextMenu , it takes only QWidget as parent.

  • Actually, it also takes a 0 pointer as a parent. You'd just need to make sure you delete the menu yourself...

  • Thanks for your help

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.