0

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 ?

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Anton Giertli
  • 896
  • 2
  • 10
  • 29
  • Your issue is not related to the tray, as minimizing the window does not involve the tray. Please show the code you use to save/restore the window geometry. – Chris Browet Mar 13 '12 at 12:17
  • My issue is related to the tray, i want to restore window with clicking on a TRAY ICON, when i previously clicked [_] button. I am able to restore window with clickin on a tray icon, when previsously pressed [X] button. And the code that handles this is in function void MainWindow::trayIconClicked(QSystemTrayIcon::ActivationReason reason); (the code is above) – Anton Giertli Mar 13 '12 at 12:39
  • Is http://stackoverflow.com/questions/3332257/how-do-i-properly-implement-a-minimize-to-tray-function-in-qt/6094872 what you're looking for? – Manjabes Mar 13 '12 at 12:48
  • Ok. Still, your issue is in what you do when restoring the window geometry. BTW, unless there are other reasons to do this, you don't have to for just minimize/maximize. This is handled by the OS window manager. – Chris Browet Mar 13 '12 at 12:53
  • Well, but i dont need to manually restore window geometry, when restoring window after clicking on [X], but as i said, i tried the code from the link ive posted in the first post, and it was no use.., Manjabes: not, thats not what im looking for, i dont want to minimize my application to trayicon when user click [ _ ],but i want be able to restore application window with clicking on tray icon, when user previously pressed [ _ ] button and send the application to the taskbar – Anton Giertli Mar 13 '12 at 12:55

1 Answers1

0

You should handle the minimization of your application in the main window's change event. One solution would be to do the same you do when the X button is pressed.

void MainWindow::changeEvent(QEvent *event)
{
    if(event->type() == QEvent::WindowStateChange) {
        if(isMinimized())
            QApplication::sendEvent(this, new QCloseEvent());  
            event->ignore();
    }
}
pnezis
  • 12,023
  • 2
  • 38
  • 38
  • This is not working for me, not only im not able to restore window with clicking on a tray icon, but when i restore window with clickin on a taskbar icon the windows is empty. Moreover, i dont want to change minimize event, i want user have two options, minimize app to taskbar with [ _ ] button, and minimize app to trayicon with [x] button. All i want to so , is that when user minimize application to taskbar with [ _ ], the restoring window with tray icon will still work. – Anton Giertli Mar 13 '12 at 12:41