[SOLVED] closing the whole application on another window



  • 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.



  • 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.



  • 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....



  • 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();
    

    }
    @



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.