Can't add menu to QSystemTrayIcon
-
Hi, I can't seem to add a menu to QSystemTrayIcon.
I've followed this tutorial, which showed me how to get the icon to the system tray, but now I can't add a context menu.
Here's what I have:tray = new QSystemTrayIcon(this); //tray declared in .h file as a class tray->setVisible(true); tray->setIcon(QIcon(":/icons/clock.png")); tray->showMessage("Screen on time", "Screen on time service enabled"); QMenu trayMenu; trayMenu.addSection("menu"); tray->setContextMenu(trayMenu);
-
@Sucharek said in Can't add menu to QSystemTrayIcon:
QMenu trayMenu;
Not my area, and I have no idea whether the whole concept works/is supposed to work. But if this your actual code (which who knows, since it's
QSystemTrayIcon::setContextMenu(QMenu *menu)
and you are passing aQMenu
not aQMenu *
so it should not compile), theQMenu trayMenu
will go out of scope and be destroyed? -
@Sucharek
I imagine you made theQMenu trayMenu
orQMenu *trayMenu = new QMenu
a member variable of some class, which persists for its desired lifetime. Note that in Qt you often have to consider this lifetime declaration for various things which need to persist; we see lots of posts where user has only created object as a method local variable and does not realise it gets destroyed on scope closure, so you are not the first to have come a cropper on this! :)