I am aware of the applications and use of modal dialogs(QDialog) and examples given all around the web, but my use is for displaying progress.
Have Tried:
As per threads/posts from here and here, I have found to use accept()
, done()
, close()
, setResult()
but none of these close the dialog, the dialog just hangs.
I have a connect()
to "click close" but this does not fully satisfy the requirements, since a successful or unsuccesful execution will in either case await user input.
update: just tried setResult(Accepted);
,QDialog remains hanging, even after successful execution.
Problem:
The modal dialog is used to show progress, at which the end of a succesful execution should close the modal dialog automatically, but if not executed successfully, the dialog should remain open for the user to view the displayed error.
Used functions:
I have used these above proposed functions. These do not close the dialog, the dialog just hangs awaiting user interaction (i.e. manaully closing it with a slot from a button)
CODE:
The dialog is executed with:
dlgConnectStatus = new LoginStatusDialog(login, key, auth);
dlgConnectStatus->setModal(true);
int res = dlgConnectStatus->exec(); << see note below
qDebug() << "dlgConnectStatus result = " << QString::number(res);
note: all code running the execution is in the dlgConnectStatus class contructor. This code is only executed once the dlgConnectStatus->exec();
is called, however no result is ever returned, the code finshes executing and hangs after the last constructor line.
I attempted adding :
if (Return_Object->getCode() == ReturnCode::netcon_LoginSuccess) {
setResult(Accepted);
accept();
}
but it still just hung!
*Finishing info (irrelevant but for putting into context):
After a code execution (successful or not), a public object is extracted from the dlgConnectStatus object before being destroyed.*
TL;DR
How can I close a QDialog, type modal, manually?
Bare/Essential Code for creating the issue
Calling Code:
dlgConnectStatus = new LoginStatusDialog(login, key, auth);
qDebug() << "Done LoginStatusDialog, setting modal";
dlgConnectStatus->setModal(true);
qDebug() << "Done setting modal, executing";
int res = dlgConnectStatus->exec();
qDebug() << "dlgConnectStatus result = " << QString::number(res);
//see below for debugger output -> the qDebug output
LoginStatusDialog.h
#ifndef LOGINSTATUSDIALOG_H
#define LOGINSTATUSDIALOG_H
#include <QDialog>
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
#include <thread>
#include "returnobject.h"
#include "datamanager.h"
namespace Ui {
class LoginStatusDialog;
}
class LoginStatusDialog : public QDialog
{
Q_OBJECT
public:
// explicit LoginStatusDialog(QWidget *parent = 0);
LoginStatusDialog( QString _login, QString _key, QString *_auth_tok, QWidget *parent = 0);
~LoginStatusDialog();
private:
Ui::LoginStatusDialog *ui;
QString login, key;
ReturnObject *Return_Object;
void initGui();
};
#endif // LOGINSTATUSDIALOG_H
LoginStatusDialog.cpp
//custom constructor
LoginStatusDialog::LoginStatusDialog( QString _login, QString _key, QString *_auth_tok, QWidget *parent) :
QDialog(parent), ui(new Ui::LoginStatusDialog), login(_login), key(_key)
{
ui->setupUi(this);
//this code is basically self explanitory
Return_Object = new ReturnObject(ReturnCode::netcon_LoginSuccess, QString(""));
if (Return_Object->getCode() == ReturnCode::netcon_LoginSuccess) {
//by use ofbreakpoints, I can verify this close is reached but not executed.
qDebug() << "pre close";
close();
qDebug() << "post close";
}
}
Debug info
Debugging starts
Creating LoginStatusDialog
pre close
post close
Done LoginStatusDialog, setting modal
Done setting modal, executing
//remains open
QWidget
based class are opened way after the constructor is called because the code that responsible to show it to the screen is called inQApplication
's application loop. I think in your caseclose()
would do nothing because the dialog has not been opened in the first place. – Leben Asa