OK, this is the code that doesn't minimize to tray when dialog is open:

mainwindow.h:
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QSystemTrayIcon>
  6. #include <QPushButton>
  7. #include <QEvent>
  8. #include <QMenu>
  9. #include <QAction>
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14. QSystemTrayIcon *trayIcon;
  15. QMenu *trayContextMenu;
  16. QAction *actShow;
  17. QPushButton *button;
  18. bool dialog_open;
  19.  
  20. public:
  21. MainWindow(QWidget *parent = 0);
  22.  
  23. protected:
  24. void changeEvent(QEvent *);
  25.  
  26. private slots:
  27. void click();
  28. void trayIcon_activated(QSystemTrayIcon::ActivationReason reason);
  29. void actShow_Triggered();
  30. };
  31.  
  32. #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. dialog_open=false;
  21. }
  22.  
  23. void MainWindow::changeEvent(QEvent *event)
  24. {
  25. if(event->type()==QEvent::WindowStateChange
  26. && dialog_open==false){
  27. if(isMinimized()){
  28. this->hide();
  29. trayIcon->show();
  30. }
  31. }
  32. else{
  33. QMainWindow::changeEvent(event);
  34. }
  35. }
  36.  
  37. void MainWindow::click()
  38. {
  39. dialog_open=true;
  40. QFileDialog::getOpenFileName(this,QString());
  41. dialog_open=false;
  42. }
  43.  
  44. void MainWindow::trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
  45. {
  46. if(reason==3){ //reason==Trigger
  47. this->show();
  48. trayIcon->hide();
  49. }
  50. }
  51.  
  52. void MainWindow::actShow_Triggered()
  53. {
  54. this->show();
  55. trayIcon->hide();
  56. }
To copy to clipboard, switch view to plain text mode