This is not a solution for the initial question but a better approach then provided by solution 1 and explains what I said in my comment to that solution:
Quote:
This is a bad solution because it uses global variables instead of dialog return values.
In your LoginDialog call accept(), reject(), or done(int r) according to the result like (these will also close the dialog):
accept() or done (QDialog::Accepted): Logged in
reject() or done (QDialog::Rejected): Not logged, no abort
done(100): Not logged in, abort
In your main window check the value returned by m_Login->exec(). It is QDialog::Accepted, QDialog::Rejected, or 100.
The '100' return code is just an example. In the below code I use
QDialogQMessageBox::Retry
instead.
The login dialog:
void LoginDialog::on_btnCancel_clicked()
{
reject();
}
void LoginDialog::on_btnLogin_clicked()
{
if (!db.open()) {
if (QMessageBox::question(this, "Login Failed",
"Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
done(QMessageBox::Retry);
}
else
{
reject();
}
}
else
{
accept();
}
}
The MainWindow function:
void MainWindow::showDialog()
{
this->hide();
int result = QMessageBox::Retry;
while (result == QMessageBox::Retry)
{
LoginDialog *login = new LoginDialog(this);
result = login->exec();
delete login;
}
if (result == QDialog::Rejected)
qApp->closeAllWindows();
else
this->show();
}
[UPDATE]
An even better solution would be to handle retry inside the login dialog:
void LoginDialog::on_btnLogin_clicked()
{
if (!db.open()) {
if (QMessageBox::question(this, "Login Failed",
"Login Failed Retry?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
return;
}
reject();
}
else
{
accept();
}
}
The corresponding MainWindow function:
void MainWindow::showDialog()
{
this->hide();
LoginDialog *login = new LoginDialog(this);
int result = login->exec();
delete login;
if (result == QDialog::Rejected)
qApp->closeAllWindows();
else
this->show();
}