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 QDialog

How to close QDialog

Scheduled Pinned Locked Moved General and Desktop
qdialogclose dialog
16 Posts 3 Posters 26.3k 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.
  • G gabor53
    31 May 2016, 03:50

    Hi,
    Which is the best way of closing a dialog?
    In my program when someone filled out a form have an option to fill out an other one or stop entering new data, in which case I need to close the dialog includes the data entry form. Please help me to find the best way. (I tried to use QDialog::~QDialog, and reject() but didn't work.)
    Thank you.

    R Offline
    R Offline
    raven-worx
    Moderators
    wrote on 31 May 2016, 10:55 last edited by
    #3

    @gabor53
    can you please be more specific than "didn't work"?
    accept()/reject()/done() should all work perfectly fine...

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • J jsulm
      31 May 2016, 04:19

      @gabor53 Isn't the user closing the dialog? Or why do you want to do it?
      If you really need to do it you can call QDialog::accepted() or reject() as you said. Why did reject() not work? What was the problem? And how do you actually call it? Can you show relevant code?

      G Offline
      G Offline
      gabor53
      wrote on 31 May 2016, 13:11 last edited by
      #4

      @jsulm
      I have 3 dialogs: mainwindow, additem and review.
      additem is opened from mainwindow using a pushbutton using the following code:

      void MainWindow::on_actionAdd_Item_triggered()
      {
          Additem *mAddItem = new Additem;
          mAddItem->exec ();
      }
      

      Additem is a data entry form. When the user finished, he has an option to click on a pushbutton and review the entries in a different dialog, called review.

      review is opened with this code:

         review  *mReview = new review(this);
         mReview->dispRev (sID,name,fileName, newWhat,newMaterial, newColor, description, month, day, year, newSignedby, history, age, notes);
      
          mReview->show ();
      

      When data is reviewed there is an option to add the data to the database:

      void review::on_submit_Button_clicked()
      {
          QMessageBox reviewMsgBox;
          reviewMsgBox.setText ("<b><font size='16' color='green'> Is everything correct? (Choosing yes will add the Friend to the database.)</font></b>");
          reviewMsgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
          int ret = reviewMsgBox.exec ();
      
          switch(ret)
          {
          case QMessageBox::Yes:
          {
      
              qDebug() << "Yes was clicked.";
              qDebug() << "Name from review.cpp: " << AdoptDater;
      
      
               Additem *mAdditem = new Additem;
      
              mAdditem->FunctAddtoDb (sIDr, namer, newWhatr, newMaterialr, newColorr, descriptionr, monthr, dayr, yearr, newSignedbyr, historyr, ager, notesr, byteArrayr);
      
      		review::close ();
          }
      
              break;
          case QMessageBox::No:
          {
              qDebug() << "No was clicked!";
              review::close ();
          }
              break;
          }
      }
      

      When it is all done and the data added to the database, and if the user does not want to add more data all data QDialogs review and Additem should be closed (the problem is they do not close). This is the code:

      QMessageBox addMoreMsgBox;
                         addMoreMsgBox.setText ("<b><font size='16' color='green'>The Friend is added to the database. Do you want to add some more?</font></b>");
                         addMoreMsgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
                         int ret = addMoreMsgBox.exec ();
      
                         switch(ret)
                         {
                         		case QMessageBox::Yes:
                         			{
                            			SubmitButton->setDisabled (false);
                            			SubmitButton->setStyleSheet ("QPushButton{"
                                                          "background-color: rgb(0, 255, 0);"
                                                          "border-style: outset;"
                                                          "border-width: 2px;"
                                                          "border-radius: 10px;"
                                                          "border-color: beige;"
                                                          "font: bold 14px;"
                                                          "min-width: 10em;"
                                                          "padding: 6px;}"
                                                          );
                             			Addcontent();
                        			 }
      
                         break;
                        		case QMessageBox::No:
                         			{
                             qDebug() << "Close from messagebox";
                         				Additem::close ();
                         }
      					}
      }
      
      void Additem::Addcontent()
      {
      
          SubmitButton->setDisabled (false);
          SubmitButton->setStyleSheet ("QPushButton{"
                                       "background-color: rgb(0, 255, 0);"
                                       "border-style: outset;"
                                       "border-width: 2px;"
                                       "border-radius: 10px;"
                                       "border-color: beige;"
                                       "font: bold 14px;"
                                       "min-width: 10em;"
                                       "padding: 6px;}"
                                       );
      
          QFont g("Arial", 16, QFont::Normal);
      
      
      J 1 Reply Last reply 1 Jun 2016, 04:14
      0
      • G gabor53
        31 May 2016, 13:11

        @jsulm
        I have 3 dialogs: mainwindow, additem and review.
        additem is opened from mainwindow using a pushbutton using the following code:

        void MainWindow::on_actionAdd_Item_triggered()
        {
            Additem *mAddItem = new Additem;
            mAddItem->exec ();
        }
        

        Additem is a data entry form. When the user finished, he has an option to click on a pushbutton and review the entries in a different dialog, called review.

        review is opened with this code:

           review  *mReview = new review(this);
           mReview->dispRev (sID,name,fileName, newWhat,newMaterial, newColor, description, month, day, year, newSignedby, history, age, notes);
        
            mReview->show ();
        

        When data is reviewed there is an option to add the data to the database:

        void review::on_submit_Button_clicked()
        {
            QMessageBox reviewMsgBox;
            reviewMsgBox.setText ("<b><font size='16' color='green'> Is everything correct? (Choosing yes will add the Friend to the database.)</font></b>");
            reviewMsgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
            int ret = reviewMsgBox.exec ();
        
            switch(ret)
            {
            case QMessageBox::Yes:
            {
        
                qDebug() << "Yes was clicked.";
                qDebug() << "Name from review.cpp: " << AdoptDater;
        
        
                 Additem *mAdditem = new Additem;
        
                mAdditem->FunctAddtoDb (sIDr, namer, newWhatr, newMaterialr, newColorr, descriptionr, monthr, dayr, yearr, newSignedbyr, historyr, ager, notesr, byteArrayr);
        
        		review::close ();
            }
        
                break;
            case QMessageBox::No:
            {
                qDebug() << "No was clicked!";
                review::close ();
            }
                break;
            }
        }
        

        When it is all done and the data added to the database, and if the user does not want to add more data all data QDialogs review and Additem should be closed (the problem is they do not close). This is the code:

        QMessageBox addMoreMsgBox;
                           addMoreMsgBox.setText ("<b><font size='16' color='green'>The Friend is added to the database. Do you want to add some more?</font></b>");
                           addMoreMsgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
                           int ret = addMoreMsgBox.exec ();
        
                           switch(ret)
                           {
                           		case QMessageBox::Yes:
                           			{
                              			SubmitButton->setDisabled (false);
                              			SubmitButton->setStyleSheet ("QPushButton{"
                                                            "background-color: rgb(0, 255, 0);"
                                                            "border-style: outset;"
                                                            "border-width: 2px;"
                                                            "border-radius: 10px;"
                                                            "border-color: beige;"
                                                            "font: bold 14px;"
                                                            "min-width: 10em;"
                                                            "padding: 6px;}"
                                                            );
                               			Addcontent();
                          			 }
        
                           break;
                          		case QMessageBox::No:
                           			{
                               qDebug() << "Close from messagebox";
                           				Additem::close ();
                           }
        					}
        }
        
        void Additem::Addcontent()
        {
        
            SubmitButton->setDisabled (false);
            SubmitButton->setStyleSheet ("QPushButton{"
                                         "background-color: rgb(0, 255, 0);"
                                         "border-style: outset;"
                                         "border-width: 2px;"
                                         "border-radius: 10px;"
                                         "border-color: beige;"
                                         "font: bold 14px;"
                                         "min-width: 10em;"
                                         "padding: 6px;}"
                                         );
        
            QFont g("Arial", 16, QFont::Normal);
        
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Jun 2016, 04:14 last edited by
        #5

        @gabor53 You did not post code for your dialogs. How are Additem and review implemented?
        Do you have any buttons user can press to close the dialogs?
        Either accept() or reject() must be called.
        And this code should actually show Additem dialog as a modal dialog:

        mAddItem->exec ();
        

        So, at least this dialog is closed, right? Else you cannot proceed with anything else.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 2 Replies Last reply 1 Jun 2016, 12:46
        0
        • J jsulm
          1 Jun 2016, 04:14

          @gabor53 You did not post code for your dialogs. How are Additem and review implemented?
          Do you have any buttons user can press to close the dialogs?
          Either accept() or reject() must be called.
          And this code should actually show Additem dialog as a modal dialog:

          mAddItem->exec ();
          

          So, at least this dialog is closed, right? Else you cannot proceed with anything else.

          G Offline
          G Offline
          gabor53
          wrote on 1 Jun 2016, 12:46 last edited by
          #6

          @jsulm
          You are right that one closes.
          Here are the codes:
          mainwindow.cpp
          additem.cpp
          review.cpp
          Thank you for your help.

          1 Reply Last reply
          0
          • J jsulm
            1 Jun 2016, 04:14

            @gabor53 You did not post code for your dialogs. How are Additem and review implemented?
            Do you have any buttons user can press to close the dialogs?
            Either accept() or reject() must be called.
            And this code should actually show Additem dialog as a modal dialog:

            mAddItem->exec ();
            

            So, at least this dialog is closed, right? Else you cannot proceed with anything else.

            G Offline
            G Offline
            gabor53
            wrote on 1 Jun 2016, 20:19 last edited by
            #7

            @jsulm
            There is a button on the review.ui they can click and close. One of my issue is that is it possible to click on close on review.ui and close both review and additem?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 2 Jun 2016, 04:15 last edited by
              #8

              You can call accept() or reject() on additem when you close review.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply 2 Jun 2016, 05:03
              0
              • J jsulm
                2 Jun 2016, 04:15

                You can call accept() or reject() on additem when you close review.

                G Offline
                G Offline
                gabor53
                wrote on 2 Jun 2016, 05:03 last edited by
                #9

                @jsulm
                Would you please show me how?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 2 Jun 2016, 05:16 last edited by
                  #10

                  First of all it will be quite unexpected behaviour if you would close addItem when user closes review! As user I would expect that if I close review only review is closed. If I understand your code correctly the flow is: add an Item via addItem, from addItem open review to review the item (optional?), then close review, close addItem. So, closing child dialog should not close parent dialog.

                  But if you really want to do it this way then just call

                  mAddItem->accept();
                  

                  You would need to pass the pointer to AddItem instance to review.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply 3 Jun 2016, 03:42
                  0
                  • J jsulm
                    2 Jun 2016, 05:16

                    First of all it will be quite unexpected behaviour if you would close addItem when user closes review! As user I would expect that if I close review only review is closed. If I understand your code correctly the flow is: add an Item via addItem, from addItem open review to review the item (optional?), then close review, close addItem. So, closing child dialog should not close parent dialog.

                    But if you really want to do it this way then just call

                    mAddItem->accept();
                    

                    You would need to pass the pointer to AddItem instance to review.

                    G Offline
                    G Offline
                    gabor53
                    wrote on 3 Jun 2016, 03:42 last edited by
                    #11

                    @jsulm
                    Unfortunately

                    mAddItem->accept();
                    

                    doesn't do anything.
                    The surrounding code:

                       QMessageBox::StandardButton reply;
                       reply = QMessageBox::information(this, "Confirmation","<b><font size='16' color='green'>The Friend is added to the database. Would you like to add more Friends?</font></b>",QMessageBox::Yes | QMessageBox::No);
                    
                           Additem *mAdditem = new Additem;
                    
                       if(reply == QMessageBox::Yes)
                       {
                           qDebug() << "Yes was clicked.";
                       }
                       else
                       {
                            mAdditem->accept ();
                       }
                    
                    
                    J 1 Reply Last reply 3 Jun 2016, 04:13
                    0
                    • G gabor53
                      3 Jun 2016, 03:42

                      @jsulm
                      Unfortunately

                      mAddItem->accept();
                      

                      doesn't do anything.
                      The surrounding code:

                         QMessageBox::StandardButton reply;
                         reply = QMessageBox::information(this, "Confirmation","<b><font size='16' color='green'>The Friend is added to the database. Would you like to add more Friends?</font></b>",QMessageBox::Yes | QMessageBox::No);
                      
                             Additem *mAdditem = new Additem;
                      
                         if(reply == QMessageBox::Yes)
                         {
                             qDebug() << "Yes was clicked.";
                         }
                         else
                         {
                              mAdditem->accept ();
                         }
                      
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 3 Jun 2016, 04:13 last edited by
                      #12

                      @gabor53 What is this code supposed to do?
                      You create an instance of Additem, but you do not show it - at least you do not call mAdditem->show() or mAdditem->exec()

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      G 1 Reply Last reply 3 Jun 2016, 05:14
                      0
                      • J jsulm
                        3 Jun 2016, 04:13

                        @gabor53 What is this code supposed to do?
                        You create an instance of Additem, but you do not show it - at least you do not call mAdditem->show() or mAdditem->exec()

                        G Offline
                        G Offline
                        gabor53
                        wrote on 3 Jun 2016, 05:14 last edited by
                        #13

                        @jsulm
                        It supposed to close additem.

                        J 1 Reply Last reply 3 Jun 2016, 05:22
                        0
                        • G gabor53
                          3 Jun 2016, 05:14

                          @jsulm
                          It supposed to close additem.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 3 Jun 2016, 05:22 last edited by
                          #14

                          @gabor53 But you do not show additem - how can it be closed?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          G 1 Reply Last reply 3 Jun 2016, 12:17
                          1
                          • J jsulm
                            3 Jun 2016, 05:22

                            @gabor53 But you do not show additem - how can it be closed?

                            G Offline
                            G Offline
                            gabor53
                            wrote on 3 Jun 2016, 12:17 last edited by
                            #15

                            @jsulm
                            Im trying to close the additem dialog that originally generated the review. So it was called from mainwindow.

                            G 1 Reply Last reply 5 Jun 2016, 03:30
                            0
                            • G gabor53
                              3 Jun 2016, 12:17

                              @jsulm
                              Im trying to close the additem dialog that originally generated the review. So it was called from mainwindow.

                              G Offline
                              G Offline
                              gabor53
                              wrote on 5 Jun 2016, 03:30 last edited by
                              #16

                              I created a new project with 2 dialogs, the sources are mainwindow.cpp and dialog.cpp.
                              The code in dialog.cpp looks like this:

                              #include "dialog.h"
                              #include "ui_dialog.h"
                              
                              Dialog::Dialog(QWidget *parent) :
                                  QDialog(parent),
                                  ui(new Ui::Dialog)
                              {
                                  ui->setupUi(this);
                              }
                              
                              Dialog::~Dialog()
                              {
                                  delete ui;
                              }
                              
                              void Dialog::message()
                              {
                                  QMessageBox::StandardButton reply;
                                     reply = QMessageBox::information(this, "Confirmation","<b><font size='16' color='green'>Do you want to close the 2nd window?</font></b>",QMessageBox::Yes | QMessageBox::No);
                              
                                     if(reply == QMessageBox::Yes)
                                     {
                                         qDebug() << "Yes was clicked. Close 2nd.";
                                         this->reject();
                                     }
                                     else
                                     {
                                         qDebug()  << "No was clicked. Don't close 2nd.";
                                     }
                              }
                              
                              void Dialog::on_pushButton_clicked()
                              {
                                  message();
                              }
                              
                              

                              When I click yes, it does what I want, it closes dialog. When I use the same code in my original application, it does nothing (no error message either). What can cause this completely different behavior? Thank you.

                              1 Reply Last reply
                              0

                              12/16

                              3 Jun 2016, 04:13

                              • Login

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