QSystemTrayIcon OSX questions
-
That's strange, as it is something that is handled by OS X itself. How does your app go out of focus? Do you hide the window (with Cmd-H) or do you close it (with Cmd-W)? If it is hidden, it should reappear. If it is closed, it can be that the application itself still runs, but has no window to show. Does the menu on the top of the screen change, when you left-click the icon in the dock? If yes, then your application is active but has no windows to show, if no something strange is going on.
-
I click the Close button on the app window, which I thought by default just hides a window (like it does on Windows). But maybe it actually closes the window? If I select "Hide" from the right click dock menu, it works fine. Do I need to do something special in order to not close the window on the Mac?
-
No, I take that back. The window handles the close event thusly:
@void visimeet::closeEvent(QCloseEvent* event)
{
if (trayIcon->isVisible() && !qApp->closingDown()) {
if ( trayWarning )
{
QMessageBox::information(this, tr("Visimeet"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
trayWarning = false;
}hide(); event->ignore(); }
}
@And I get the warning message and the window goes away.
-
There seems to be a difference if the window is hidden programmatically with hide() or by the window system with Cmd-H. In the first case the window is hidden permanently and can only be restored with calling show() again, whereas in the second case the window can be restored by clicking on the dock icon.
You can achieve a similar behaviour if you call showMinimized() instead of hide(). In this case you get an additional icon in the dock for the hidden window (in addition to the application's icon).
-
Programs can hide themselves on the mac, just use hide(). The only drawback of this method is that they cannot be restored using the dock.
You can of course add a menu to the dock icon, and in the slots, that menu is connected to, you can redisplay the window by calling show().
How to do this in detail depends on your application design: do you have multiple main windows of the same type, multiple main windows of different types that can only be instantiated once, only one main window, etc.? This influences how and where the dock menu is populated and its actions are executed.
-
I had a similar problem. I solved it by instead of calling hide() when the user clicks x (see tray sample) we call
ProcessSerialNumber pn;
GetFrontProcess (&pn);
ShowHideProcess(&pn,false);Now the window is closed and clicking on the dock icon works as expected.
-
On Mac OS X, the tray is usually the area in the top right corner of the main screen.
The tray in OS X does not support min/max/hide functions, but provides a menu to common functions, that should be available, even when the app is not in the foreground (such as setting the status in some instant messaging program or something like that).
Bringing the application to the forground/minimizing/maximizing is done in the Dock on the mac (the collection of icons, usually on the bottom of the main screen).
This is a sample code that works for me:
@
QSystemTrayIcon *sti = new QSystemTrayIcon(this);QIcon xmediaIcon("/path/to/your/icon");
sti->setIcon(xmediaIcon);
sti->setVisible(true);QMenu *stiMenu = new QMenu(this);
sti->setContextMenu(stiMenu);QAction *a1 = new QAction("tray menu item 1", this);
stiMenu->addAction(a1);
connect(a1, SIGNAL(triggered()), this, SLOT(action1Fired()));QAction *a2 = new QAction("tray menu item 2", this);
stiMenu->addAction(a2);
connect(a2, SIGNAL(triggered()), this, SLOT(action2Fired()));
@@goetz hi, I know this topic is old but i have problem with macos when qsysteemtrayicon->showmessage().
Icon appear in top menu but notification message disappear. Dose any permission or something thing in macos to show notification message because window work fine.