Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] closing the whole application on another window
QtWS25 Last Chance

[SOLVED] closing the whole application on another window

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 8.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xeroblast
    wrote on last edited by
    #1

    hi,

    ive been trying to do this half-of-my-day already but to no avail. i want to close/quit the whole application in a child dialog but the main window isnt close.

    #mainwindow
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    MainWindow::openDialog();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::openDialog()
    {
    Dialog *d = new Dialog();
    if (d->exec() == 0) {
    qApp->quit();
    }
    }
    @

    the child window
    @
    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    connect(ui->pushButtonReject, SIGNAL(clicked()), this, SLOT(reject()));
    connect(ui->pushButtonAccept, SIGNAL(clicked()), this, SLOT(accept()));
    }

    Dialog::~Dialog()
    {
    delete ui;
    }
    @

    so when i click the Reject button, the whole application should close but the main window is still open. what went wrong?
    the Accept button should keep the main window open. how do i achieve this?

    thanx.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xeroblast
      wrote on last edited by
      #2

      hello again, i came up with something like this

      #mainwindow
      @
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "dialog.h"

      MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
      {
      if (MainWindow::openDialog()) {
      ui->setupUi(this);
      } else {
      qApp->quit();
      }
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }

      bool MainWindow::openDialog()
      {
      Dialog *d = new Dialog(this);
      return d->exec();
      }
      @

      #child window
      @
      #include "dialog.h"
      #include "ui_dialog.h"

      Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
      {
      ui->setupUi(this);
      connect(ui->pushButtonReject, SIGNAL(clicked()), this, SLOT(reject()));
      connect(ui->pushButtonAccept, SIGNAL(clicked()), this, SLOT(accept()));
      }
      @

      the Accept button works perfectly but the Reject button is not. there is still a small empty dialog when i click the Reject button.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Robbin
        wrote on last edited by
        #3

        I don't know what your idea is, so not sure if that would work for you, but why don't you try using "QMessageBox":http://doc.qt.nokia.com/latest/qmessagebox.html instead? You can grab the output of the buttons clicked in your app and based on that quit, save, etc....

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          Why do you open a MainWindow in the first place? Just open the dialog before you create and show the main window.
          @
          int main(int argc, char* argv[])
          {
          QApplication application(argc, argv);

          Dialog dialog;
          if(dialog.exec() == 0)
              return -1;
          
          MainWindow mainWindow;
          mainWindow.show();
          
          return application.exec();
          

          }
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            If you call quit() on a QApplication before you started the main event loop by calling app.exec(), the call to quit does nothing. This means, that when you eventually return to your main function and call app.exec() it will be the same as if app.quit() has never been called.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • X Offline
              X Offline
              xeroblast
              wrote on last edited by
              #6

              thanks Volker for the explanation. i get it now why the quit() doesnt work. i was stuck on that.

              Lukas Geyer : thanks, i was thinking about last night that editing my main.cpp would do the trick.

              Eus : im doing a login dialog first, if dialog is cancel/close as so as the mainwindow.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved