Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] closing the whole application on another window

    General and Desktop
    4
    6
    8337
    Loading More Posts
    • 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
      xeroblast last edited by

      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 Reply Quote 0
      • X
        xeroblast last edited by

        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 Reply Quote 0
        • R
          Robbin last edited by

          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 Reply Quote 0
          • L
            lgeyer last edited by

            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 Reply Quote 0
            • G
              goetz last edited by

              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 Reply Quote 0
              • X
                xeroblast last edited by

                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 Reply Quote 0
                • First post
                  Last post