Don't close the window when clicking "OK" QDialog
-
Hello everyone, I want the window to not close when the "OK" button is pressed. Here is the code:
#include "authorizationview.h" #include "qmessagebox.h" #include "ui_authorizationview.h" AuthorizationView::AuthorizationView(QWidget *parent) : QDialog(parent), ui(new Ui::AuthorizationView) { ui->setupUi(this); connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo())); } AuthorizationView::~AuthorizationView() { delete ui; } void AuthorizationView::ResultAuthorization(bool result) { if (result) { this->close(); } else { QMessageBox::information(this, "Result Authorization", "Incorrect login or password"); } } void AuthorizationView::SendInfo() { emit InfoAuth(qMakePair(ui->login->text(), ui->password->text())); }
-
@Idodoqdo Override https://doc.qt.io/qt-6/qdialog.html#accept - in your override set the result to Accepted (https://doc.qt.io/qt-6/qdialog.html#setResult), but don't hide the window. Also do not call inherited accepted() method inside of your override.
But I'm wondering why you want to do this? If I as user click OK button in a dialog I expect it to close or at least to provide some information why it is not possible.
-
@jsulm this help me
#include "authorizationview.h" #include "qmessagebox.h" #include "ui_authorizationview.h" AuthorizationView::AuthorizationView(QWidget *parent) : QDialog(parent), ui(new Ui::AuthorizationView) { ui->setupUi(this); connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo())); } AuthorizationView::~AuthorizationView() { delete ui; } void AuthorizationView::ResultAuthorization(bool result) { if (result) { this->close(); } else { QMessageBox::information(this, "Result Authorization", "Incorrect login or password"); } } void AuthorizationView::SendInfo() { emit InfoAuth(qMakePair(ui->login->text(), ui->password->text())); } void AuthorizationView::accept() { setResult(QDialog::Accepted); }
Thx
-
@Idodoqdo said in Don't close the window when clicking "OK" QDialog:
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));
I am somewhat surprised this works. What type is
ui->buttonBox
? It does not help that you are using the old styleSIGNAL
/SLOT()
macros toconnect()
, which were deprecated a decade ago. -
@Idodoqdo said in Don't close the window when clicking "OK" QDialog:
and what is the new way to connect?
-
@JonB said in Don't close the window when clicking "OK" QDialog:
That is much better/clearer/safer. It will also tell you if anything is wrong at compile-time. Basically for your purposes you should continue to do connect()s using this syntax all the time.
thx
-
Does using a QDialogButtonBox within a ui file automatically connect its signals with the surrounding QDialog? I have created a QDialog and a QDialogButtonBox with code instead and there I need to connect the signals myself in order to have the buttons of the QDialogButtonBox do anything at all.
-
@SimonSchroeder
Is this not what the OP'sconnect(ui->buttonBox, &QDialogButtonBox::accepted, ...)
already does? -
@JonB said in Don't close the window when clicking "OK" QDialog:
Is this not what the OP's connect(ui->buttonBox, &QDialogButtonBox::accepted, ...) already does?
Usually, you would connect to QDialog::accept. So, somewhere I would expect that the dialog is closed on purpose if there is no automatic connection between QDialogButtonBox::accepted and QDialog::accept. Why else would it close?
-
@SimonSchroeder
Ah, I think I understand you now. I haven't used aQDialogButtonBox
. You don't mind theQDialogButtonBox::accepted
signal having a slot, but you would expect that to accept the dialog and then the dialog'saccepted
to do the action, right? Likeconnect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(this, &QDialog::accepted, this, &AuthorizationView::SendInfo);
Is this what you had in mind?
But I think there is a wrinkle. The OP's question is "Don't close the window when clicking "OK" QDialog", which the above would do? I think they want the authorization to happen upon accepting/clicking the "OK" button in the dialog button box but before the dialog gets accepted. Then they verify the data and only accept the overall dialog if that has succeeded. I think that is why they do not attach
QDialogButtonBox::accepted
toQDialog::accept
? -
@JonB I'm not saying you should connect those (certainly not if you don't want the dialog to close upon clicking 'OK'). I was saying that either there is some auto-connect because of the ui-file (which I don't know) or somewhere in the code the dialog is closed on purpose. If the OP thinks he's not closing the dialog himself on purpose, then the auto-connect would be one of my guesses to check.
-
@SimonSchroeder said in Don't close the window when clicking "OK" QDialog:
or somewhere in the code the dialog is closed on purpose. If the OP thinks he's not closing the dialog himself on purpose
In the OP's
void AuthorizationView::ResultAuthorization(bool result)
he hasif (result) { this->close();
His code emits a signal with the name & password, the authorizer presumably calls above method with success/failure parameter for authorization. If success it closes the dialog, but if failure it puts up a message box but stays inside the dialog, presumably for the user to try again. That is why the button box's "OK"/accepted should not be connected directly and unconditionally to the dialog's accepted.