SOLVED: Quit Application when Dialog closes



  • Hello,

    I have been trying to implement a login dialog for my new App but can't get the whole program to quit if the dialog window is closed by a user without authentication.
    I am trying to overwrite the closeEvent() function for the dialog but nothing happens when its closed.
    Below is the code for main.cpp, and dialog.cpp, the rest of the source & header files are the default for a new Qt MainWindow project.

    main.cpp
    @#include "mainwindow.h"
    #include "dialog.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    //bring dialog forward at the start of the application
    Dialog box;
    box.setModal(true);
    box.exec();

    return a.exec(&#41;;
    

    }@

    dialog.cpp
    @#include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    }

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

    //overwrite function for Dialog
    void Dialog::closeEvent(QCloseEvent *)
    {
    qApp->quit();
    }@

    I declared the prototype void Dialog::closeEvent(QCloseEvent *) in dialog.h as public. Please advise me where am wrong, and is a dialog the best way to implement a Login feature?

    Thanks in Advance



  • Qkato,

    Instead of closing the application that way, it would be better to do something like this:

    @switch ( dialog.exec() )
    {
    case QMessageBox::Cancel:
    return 0;
    }

    return app.exec()@

    Or something similar. Hope this helps.



  • Hi,

    If I were you:

    • I would implement 'login' inside MainWindow as an option.

    • The MainWindow would be partially 'disabled' waiting for login, and fully 'enabled' (depending on user rights) after login.

    • If you really want to 'quit' after a 'invalid' login you just have to 'close' the MainWindow.

    • When you manage to put this (login) working manually, later (if necessary) you can find a way to trigger it automatically after the MainWindow is created.

    Hope this helps.



  • Thanks bradenedmunds,

    I put the switch statement inside int main() and it worked fine except that it stops working when I delete the closeEvent() block



  • Thanks for the reply antseq, but how exactly do I implement the login inside MainWindow?
    [quote author="antseq" date="1370294153"]Hi,

    If I were you:

    • I would implement 'login' inside MainWindow as an option.

    • The MainWindow would be partially 'disabled' waiting for login, and fully 'enabled' (depending on user rights) after login.

    • If you really want to 'quit' after a 'invalid' login you just have to 'close' the MainWindow.

    • When you manage to put this (login) working manually, later (if necessary) you can find a way to trigger it automatically after the MainWindow is created.

    Hope this helps.[/quote]



  • [quote author="Qkato" date="1370295945"]Thanks for the reply antseq, but how exactly do I implement the login inside MainWindow?[/quote]

    generally speaking:

    • usually a MainWindow is 'supposed' to be the window (starting point, interface) where users interact directly with you application

    • usually a MainWindow have a few (or a lot) options : Menu options, Toolbar options, Touch (screen) options or whatever you design to let the user "click" or "select" the desired command.

    • you just have to add a new option/command to let the user 'login' into the system

    • in response of user "clicking" or "selecting" this 'login' command, that's when you will show your 'login dialog'

    • meanwhile the user has not logged in... you should keep the other MainWindow options disabled.

    • It's just like when you are using a word processor and you close the document and get limited access to several menu options because you do not have a (working) document. As soon as you open a new document, you will have full access to all menu options.



  • Thanks antseq, definitely going to look into having a login dialog from within MainWindow itself.


Log in to reply
 

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