0
votes

I've stumbled across very strange behaviour during work on my program. I've written custom changeEvent class, which allows me to hide program to SysTray on minimizing. But when i double click on taskbar app icon, the function goes crazy. It creates 2 to 4 systray icons and on requesting window show again, it just shows main window borders without any content inside.

Here's my changeEvent code:

void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
if(e->type()==QEvent::WindowStateChange)
    if(isMinimized()) {
        trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
        connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));

        QAction *showAction=new QAction("Pokaż",trayIcon);
        connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));

        QMenu *trayIconMenu=new QMenu;
        trayIconMenu->addAction(showAction);

        trayIcon->setContextMenu(trayIconMenu);
        trayIcon->show();

        this->hide();
    }

}

on_show(QSystemTrayIcon::ActivatioReason) SLOT:

void MainWindow::on_show(QSystemTrayIcon::ActivationReason reason) {
    if(reason) {
        if(reason!=QSystemTrayIcon::DoubleClick)
            return;
    }
    if(this->isMinimized()) {
        this->raise();
        this->showNormal();
        this->setWindowState(Qt::WindowActive);
        trayIcon->hide();
    }
}

on_show() SLOT is just the same besides that first if.

Soo, I would like to know whether there is any way to disable minimizing of window by taskbar icon click. If there's none, then maybe you have any ideas what can go wrong in here when doubleclicking on icon in taskbar?

Thanks for help!

1
can you share on_show SLOT ? - uchar
Here, I've edited main post. - Theoden91
for one thing it looks like you're leaking QSystemTrayIcon since you make a new one every time the thing is minimized, seemingly without cleaning up. Also, in on_show you set the window state, which is probably causing changeEvent to be called again, with the window still minimized. - Nicolas Holthaus

1 Answers

0
votes

I've managed to work around that problem by overloading closeEvent function and leaving alone changeEvent function.

So, I'm using boolean flag to distinct between closing of program by menu item and by clicking "X" button and the rest stays just the same, as posted in my earlier post with one change.

I've moved this whole block of code to window constructor in order to prevent multiple creation of trayIcon, as pointed out by Nicolas.

    trayIcon=new QSystemTrayIcon(QIcon(":/icon/itime.ico"));
    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_show(QSystemTrayIcon::ActivationReason)));

    QAction *showAction=new QAction("Pokaż",trayIcon);
    connect(showAction,SIGNAL(triggered()),this,SLOT(on_show()));

    QMenu *trayIconMenu=new QMenu;
    trayIconMenu->addAction(showAction);

    trayIcon->setContextMenu(trayIconMenu);

Thanks for your help!