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. How to close Message Box Window

How to close Message Box Window

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.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.
  • S Offline
    S Offline
    saurabh162
    wrote on 21 Feb 2019, 15:48 last edited by
    #1

    Hello Developers,

    I open Message Box window using following code and want to close it after user has pressed either yes or no button. Can you please inform me how can I do it.

    void MainWindow::on_pushButton_clicked()
    {
      
        QMessageBox::StandardButton reply = QMessageBox::question(this,
                                            "My Title", "This is my custom Message",
                                            QMessageBox::Yes | QMessageBox::No);
        if(reply == QMessageBox::Yes){
           // QApplication::quit();
            //QMessageBox::Close();
            qDebug() << "Yes is clicked";
        }
        else
        {
    
            qDebug() << "No is clicked";
        }
    

    Thanks a lot :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Feb 2019, 22:26 last edited by
      #2

      Hi,

      The QMessageBox will be automatically closed when clicking on either of these buttons. Isn't that the case for you ?

      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
      2
      • S Offline
        S Offline
        saurabh162
        wrote on 22 Feb 2019, 08:23 last edited by saurabh162
        #3

        Hello @SGaist

        Thank you very much for your fast reply. I think it was closing but my main window was not refreshing fast due to which I could not see closing of Message Box.

        In other words, I could see closing of Message box only after I have put some milliseconds delay (generated using single shot timer) before checking value of variable "reply" as given below.

        void MainWindow::on_pushButton_clicked()
        {
          
            QMessageBox::StandardButton reply = QMessageBox::question(this,
                                                "My Title", "This is my custom Message",
                                                QMessageBox::Yes | QMessageBox::No);
          DelayMilliseconds(200); 
           if(reply == QMessageBox::Yes){
               // QApplication::quit();
                //QMessageBox::Close();
                qDebug() << "Yes is clicked";
            }
            else
            {
        
                qDebug() << "No is clicked";
            }
        

        Please explain me if I have made any mistake or not understanding it correctly.
        Thanks a lot :)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Feb 2019, 22:29 last edited by
          #4

          What refreshing are you talking about ?

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

          P 1 Reply Last reply 23 Feb 2019, 17:05
          2
          • S SGaist
            22 Feb 2019, 22:29

            What refreshing are you talking about ?

            P Offline
            P Offline
            Pl45m4
            wrote on 23 Feb 2019, 17:05 last edited by Pl45m4
            #5

            @SGaist

            I think he meant, that if he uncomment the "QApplication::quit", he does not see the result of the MSG-Box, because the main program gets killed instantly. So he cant check the result (output) of the if-clause.
            Which makes sence :)

            @saurabh162
            You dont need any delay.
            Just keep the code as it is. Just remove the "QApplication::quit" and the "QMessageBox::Close", as the MessageBox gets closed after ANY ButtonPress Event (YES, NO, CANCEL, SAVE...whatever...)
            Your MessageBox behaves like it should.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            2

            1/5

            21 Feb 2019, 15:48

            • 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