title: Closing Dialog quits Application if QMainWindow is hidden
body: <p>I have the following Problem:</p>
  
  <p>I have a Qt MainWindow which starts several Dialogs. Through an external source, I can hide and show the MainWindow. If a dialog is open, only the MainWindow is hidden, but the dialog is still visible. This is not nice but not my main problem. The main problem is, if i close the dialog while the MainWindow is hidden, the whole Application terminates. I do not want that, because I can make my main window visible again by external source.</p>
  
  <p>I know it has something to do with QApplication quitOnLastWindowClosed. But if i set it true, my Application doesn't terminate if i normaly press "X".</p>
  
  <p>Here is an example:</p>
  
  <pre><code>// MainApp.h
  #include &lt;QMainWindow&gt;
  #include "ui_MainWindow.h"
  
  class MainApp : public QObject {
    Q_OBJECT
  public:
    MainApp(QObject *parent = nullptr);
  private slots:
    void slotOpenDialog();
  private:
    QMainWindow mMainWindow;
    Ui::MainWindow mUi;
  };
  
  // MainApp.cpp
  #include "MainApp.h"
  #include &lt;QTimer&gt;
  #include &lt;QMessageBox&gt;
  
  MainApp::MainApp(QObject *parent) {
    mUi.setupUi(&amp;mMainWindow);
    mMainWindow.show();
  
    connect(mUi.pushButton, &amp;QPushButton::clicked, this, &amp;MainApp::slotOpenDialog);
  
    // simulate external hide and show mechanism
    QTimer *timer = new QTimer(this);
    connect(timer, &amp;QTimer::timeout,
            [=] {
              if(mMainWindow.isHidden()) mMainWindow.show();
              else mMainWindow.hide();
            });
    timer-&gt;start(3000);
  }
  
  void MainApp::slotOpenDialog() {
    QMessageBox::information(nullptr, "Info", "text");
  }
  
  // main.cpp
  #include &lt;QApplication&gt;
  #include "MainApp.h"
  
  int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainApp* mApp = new MainApp;
    // if set true, I can't exit the application with "X"
    //a.setQuitOnLastWindowClosed(false);
    int error = a.exec();
    delete mApp;
    return error;
  }
  </code></pre>
  
  <p>How can I prevent the program from quitting when it is hidden and a still visible dialog has been closed and how can I make it exit normally when the window is visible?</p>