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. Exit of application from GUI (QMessageBox) not working

Exit of application from GUI (QMessageBox) not working

Scheduled Pinned Locked Moved Solved General and Desktop
qapplication
5 Posts 4 Posters 4.4k 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.
  • R Offline
    R Offline
    RolBri
    wrote on 17 Nov 2015, 15:24 last edited by
    #1

    Hi,

    I would like to have a message box at the program start with a "yes" and a "no" button.
    If "no" is pressed the software should quit.

    I tried this in the following way:

    void MainWindow::discalimer()
    {
        QMessageBox msgBox(
                    QMessageBox::Warning,
                    trUtf8("Warning"),
                    "text",
                    QMessageBox::Yes | QMessageBox::No);
    
        msgBox.setButtonText(QMessageBox::Yes, trUtf8("Yes!"));
        msgBox.setButtonText(QMessageBox::No, trUtf8("No!"));
        connect( msgBox.button(QMessageBox::No), SIGNAL(clicked()) ,qApp, SLOT(quit()) );
        msgBox.exec();
    }
    

    This function I call in the constructor of my MainWindow after ui->setupUi(this);

    I also tried to call the function somewhere else but my software always starts.
    Why is that and how can I solve it?

    Sadly I could not find a solution with google :-/

    Thank you very much :-)

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bsomervi
      wrote on 17 Nov 2015, 18:00 last edited by
      #2

      One way of doing this is to call your MainWindow::close() and then connect the QApplication::lastWindowClosed() signal to QApplication::quit().

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jeroentjehome
        wrote on 17 Nov 2015, 18:49 last edited by Jeroentjehome
        #3

        Hi,
        Calling a .exec in the constructor of your MainWindow won't work! The event handler of the QApplication is not active at that time.
        First create your MainWindow, then start your QApplication (in your main() function).
        But then your MainWindow will be visible if you do not set it hidden. Create a messageBox in a single shot timer and you should be good.
        Maybe an other way is to use a SplashScreen. No idea if you are able to use button in that one.
        Nup, no buttons, but you could use the buttonpressed, so set a timer with e.g. 5 seconds. If you do not click the splashscreen, start the program. IF you do click it, stop it?
        But the first option is better IYAM.

        Greetz, Jeroen

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Nov 2015, 21:41 last edited by
          #4

          Hi,

          To add to @Jeroentjehome, if you need to quit the application before the MainWindow is constructed then why not use that QMessageBox in main before constructing your MainWindow ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Offline
            R Offline
            RolBri
            wrote on 18 Nov 2015, 07:43 last edited by
            #5

            Thank you all very much.

            I now created a QMessageBox in the main.cpp before constructing the MainWindow as SGaist suggested.
            If "no" is pressed calling QApplication::quit() does nothing, but just return 0 works perfectly fine :-)

            1 Reply Last reply
            0

            1/5

            17 Nov 2015, 15:24

            • Login

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