I am developing QT application and I have a problem with restoring window from trayicon.
Here is a code connected with tray icon :
createActions();
createTrayIcon();
setIcon();
trayIcon->show();
connect(
trayIcon,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,
SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))
);
create actions
open = new QAction(tr("&Open"), this);
connect(open, SIGNAL(triggered()), this, SLOT(show()));
close = new QAction(tr("&Quit"), this);
connect(close, SIGNAL(triggered()),qApp, SLOT(quit()));
void MainWindow::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(open);
trayIconMenu->addSeparator();
trayIconMenu->addAction(close);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::Trigger)
this->show();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QString title = "Smart Typer 2000";
QString text = "Aplikácia stále beží, obnovíte ju poklepením po ikonke, zrušíte ju stlačením ikonky pravým tlačítkom,"
"a následným zvolením možnosti Quit";
if (trayIcon->isVisible()) {
trayIcon->showMessage(title,text,QSystemTrayIcon::Information,5000);
trayIcon->setToolTip("Smart Typer 2000");
this->setVisible(false);
event->ignore(); // Don't let the event propagate to the base class
}
}
So, when i click [x] button in mainwindow, application is send to trayicon, when i click on this trayicon, the mainwindow size and position is restored, which is good.
But when i press [_] button, application is minimized, both task bar icon and system tray icon are visible, and after clicking on tray icon, the application main window wont restore itself. (also window wont restore itself even if i click on trayicon with right button and then select "Open" option)
I tried various things like, instead of
this->show();
I tried call
this->showNormal();
the only time, when window was restored, was when I called
this->showMaximized();
of course, the main window was really maximized, which was not very nice, so I set max width and max height to the default application dimensions (I didnt mind it, because my application does not need to be any bigger), but... although window was restored, and the size was good, the position was not , window was restored in the upper left corner of my monitor, which kinda suck. So I tried use this piece of code http://doc.qt.io/archives/qt-4.7/restoring-geometry.html
I saved windows state and geometry on hide and close events, then I tried to restore it when trayicon was clicked, but with no success..
So, do you have any ideas ? How to restore window by clicking on trayicon when window was minimized ?