I am working on a program that can minimize to tray and hide the main window. I expect the mainwindow to show when I click on the tray icon. This usually works, but I find that if the program minimizes to tray when I call QFileDialog::getOpenFileName() to select file without closing the dialog, I cannot activate the trayicon. Does anyone know how to solve it? I am using Qt 5.12.1 on Manjaro Linux. Here is my example:

main.cpp:

Qt Code:
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.show();
  9.  
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

mainwindow.h:

Qt Code:
  1. class MainWindow : public QMainWindow
  2. {
  3. Q_OBJECT
  4. QSystemTrayIcon *trayIcon;
  5. QMenu *trayContextMenu;
  6. QAction *actShow;
  7. QPushButton *button;
  8.  
  9. public:
  10. MainWindow(QWidget *parent = 0);
  11.  
  12. protected:
  13. void changeEvent(QEvent *);
  14.  
  15. private slots:
  16. void click();
  17. void trayIcon_activated(QSystemTrayIcon::ActivationReason reason);
  18. void actShow_Triggered();
  19. };
  20.  
  21. #endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode 

mainwindow.cpp:

Qt Code:
  1. #include "mainwindow.h"
  2. #include <QFileDialog>
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent)
  6. {
  7. QPushButton *button = new QPushButton("button", this);
  8. setCentralWidget(button);
  9. connect(button,&QPushButton::clicked,
  10. this,&MainWindow::click);
  11. trayIcon=new QSystemTrayIcon;
  12. trayIcon->setIcon(QIcon("hmtimer.png"));
  13. trayContextMenu=new QMenu;
  14. actShow=trayContextMenu->addAction(tr("Show"));
  15. trayIcon->setContextMenu(trayContextMenu);
  16. connect(actShow,&QAction::triggered,
  17. this,&MainWindow::actShow_Triggered);
  18. connect(trayIcon,&QSystemTrayIcon::activated,
  19. this,&MainWindow::trayIcon_activated);
  20. }
  21.  
  22. void MainWindow::changeEvent(QEvent *event)
  23. {
  24. if(event->type()==QEvent::WindowStateChange){
  25. if(isMinimized()){
  26. this->hide();
  27. trayIcon->show();
  28. }
  29. }
  30. else{
  31. QMainWindow::changeEvent(event);
  32. }
  33. }
  34.  
  35. void MainWindow::click()
  36. {
  37. QFileDialog::getOpenFileName(this,QString());
  38. }
  39.  
  40. void MainWindow::trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
  41. {
  42. if(reason==3){ //reason==Trigger
  43. this->show();
  44. trayIcon->hide();
  45. }
  46. }
  47.  
  48. void MainWindow::actShow_Triggered()
  49. {
  50. this->show();
  51. trayIcon->hide();
  52. }
To copy to clipboard, switch view to plain text mode 

The example can be downloaded here

It seems that if the program minimizes to tray when QFileDialog::getOpenFileName(this,QString()) is not closed, I cannot make the mainwindow appear by click on the tray icon or right click to show the context menu.

How to reproduce:

1. Open the program
2. Click on the button to open file dialog
3. Minimize to tray
4. Try clicking and right-clicking on the tray icon