How to close QDialog
-
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.