How to close QDialog
-
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. -
@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? -
@gabor53
can you please be more specific than "didn't work"?
accept()/reject()/done() should all work perfectly fine... -
@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);
-
@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.
-
@jsulm
You are right that one closes.
Here are the codes:
mainwindow.cpp
additem.cpp
review.cpp
Thank you for your help. -
You can call accept() or reject() on additem when you close review.
-
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.
-
@jsulm
UnfortunatelymAddItem->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 (); }
-
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.