How do I properly implement a “minimize to tray” function in Qt?

后端 未结 4 1614
抹茶落季
抹茶落季 2020-12-08 11:36

How do I properly implement a \"minimize to tray\" function in Qt?

I tried the following code inside QMainWindow::changeEvent(QEvent *e), but the window

4条回答
  •  悲哀的现实
    2020-12-08 12:09

    Apparently a small delay is needed to process other events (perhaps someone will post the exact details?). Here's what I ended up doing, which works perfectly:

    void MainWindow::changeEvent(QEvent* e)
    {
        switch (e->type())
        {
            case QEvent::LanguageChange:
                this->ui->retranslateUi(this);
                break;
            case QEvent::WindowStateChange:
                {
                    if (this->windowState() & Qt::WindowMinimized)
                    {
                        if (Preferences::instance().minimizeToTray())
                        {
                            QTimer::singleShot(250, this, SLOT(hide()));
                        }
                    }
    
                    break;
                }
            default:
                break;
        }
    
        QMainWindow::changeEvent(e);
    }
    

提交回复
热议问题