[solved] QDialog closing
-
Hi everybody,
my problem is that when I click on the OK button on the QDialogButtonBox of my dialog, I have to verify data inserted in the dialog and if there's something wrong, show a messagebox and avoid closing the dialog.
now I'm handling the accept() slot, and it seems it's impossible to stop closing when clicked on the button.
Is there a way to avoid window closing?
thanks
Paolo -
Probably you should re-write "QDialog::closeEvent":http://doc.qt.nokia.com/latest/qdialog.html#closeEvent and ignore this event if you wish.
-
You can override closeEvent and add your additional test code before accept the closing.
@void MyDialog::closeEvent(QCloseEvent *event) {
if (maybeSave()) {
writeSettings();
event->accept(); // close window
} else {
event->ignore(); // keep window
}
}@ -
if you use QPushButton, than you can connect signal to you slot, and then do validation, if your data is valid, then exec QDialog::accept() slot, else - do nothing :)
-
or if you use QDialogButtonBox you can do like that:
@switch (m_ui.btnBox->buttonRole(btnClicked)){
case QDialogButtonBox::ResetRole:
generateDefaultValues();
updateRows();
break;
case QDialogButtonBox::AcceptRole:
if (isValid()) {
s_rows = m_rows;
QDialog::accept();
}
break;
case QDialogButtonBox::RejectRole:
QDialog::reject();
break;
}@