title: Qt calling close() closes the MainWindow instead of the dialog window
body: <p>In my sample application, the MainWindow closes instead of the dialog window. Why is that and how do I change it?</p>
  <p>I've tried <code>setQuitOnLastWindowClosed()</code> but it didn't have any effect.</p>
  <p>When clicking the x in the top right corner, the dialog window closes and the MainWindow remains open as expected. But that's of course not what I want.</p>
  <p>MainWindow.h:</p>
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QDialog&gt;
  #include &lt;QWidget&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QHBoxLayout&gt;
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  private:
      void on_mainWindowBtn();
      void on_closeBtn();
  
  public:
      MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
  };
  #endif // MAINWINDOW_H
  </code></pre>
  <p>MainWindow.cpp:</p>
  <pre><code>#include &quot;mainwindow.h&quot;
  
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
  {
      QWidget *mainWindowWidget = new QWidget(this);
      QHBoxLayout *mainWindowHBox = new QHBoxLayout();
      QPushButton *mainWindowPushButton = new QPushButton();
      mainWindowPushButton-&gt;setText(&quot;Open Dialog Window&quot;);
      mainWindowHBox-&gt;addWidget(mainWindowPushButton);
      mainWindowWidget-&gt;setLayout(mainWindowHBox);
      this-&gt;setCentralWidget(mainWindowWidget);
  
      connect(mainWindowPushButton, &amp;QPushButton::released, this, &amp;MainWindow::on_mainWindowBtn);
  }
  
  void MainWindow::on_mainWindowBtn()
  {
      QDialog *newDialog = new QDialog();
      QHBoxLayout *newHBox = new QHBoxLayout();
      QPushButton *closeBtn = new QPushButton();
      closeBtn-&gt;setText(&quot;Close Dialog Window&quot;);
      newHBox-&gt;addWidget(closeBtn);
      newDialog-&gt;setLayout(newHBox);
      newDialog-&gt;setModal(true);
  
      connect(closeBtn, &amp;QPushButton::released, this, &amp;MainWindow::on_closeBtn);
  
      newDialog-&gt;exec();
  }
  
  void MainWindow::on_closeBtn()
  {
      close();     // closes the MainWindow but not the dialog window
  }
  
  MainWindow::~MainWindow()
  {
  }
  </code></pre>
  <p>main.cpp:</p>
  <pre><code>#include &quot;mainwindow.h&quot;
  
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      a.setQuitOnLastWindowClosed(false);
      MainWindow w;
      w.show();
      return a.exec();
  }
  </code></pre>