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: Quit Application when Dialog closes

SOLVED: Quit Application when Dialog closes

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.4k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    Qatto
    wrote on last edited by
    #1

    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

    Web/Desktop Developer

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bradenedmunds
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antseq
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qatto
          wrote on last edited by
          #4

          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

          Web/Desktop Developer

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qatto
            wrote on last edited by
            #5

            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]

            Web/Desktop Developer

            1 Reply Last reply
            0
            • A Offline
              A Offline
              antseq
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qatto
                wrote on last edited by
                #7

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

                Web/Desktop Developer

                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