简体   繁体   中英

Modal Dialog hangs after accept, done, or close is called

I have a modal dialog called by:

qDebug() << "Creating LoginStatusDialog";
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 debug info and output

This calls a custom constructor which executes this below

LoginStatusDialog.cpp (extract):

LoginStatusDialog::LoginStatusDialog( QString _login,  QString _key, QString *_auth_tok, QWidget *parent) :
    QDialog(parent), ui(new Ui::LoginStatusDialog), login(_login), key(_key)
{
    ui->setupUi(this);
    Return_Object = new ReturnObject(ReturnCode::netcon_LoginSuccess, QString(""));
    if (Return_Object->getCode() == ReturnCode::netcon_LoginSuccess) {
        qDebug() << "pre close";
        close();
        //accept - hangs
        //done(0); - hangs
        qDebug() << "post close";
    }
}

LoginStatusDialog header

#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

Debug info

Debugging starts
Creating LoginStatusDialog
pre close
post close
Done LoginStatusDialog, setting modal
Done setting modal, executing
//remains open

By doing some research, several SO posts refer to using done() , close() , accept() to close the dialog

Using a CONNECT() uses the close() method to close the dialog, but attempting to manually close it has no success.

It was suggested to use a timer task, but that just does not seem like the actual way to close the dialog.

Any thoughts?

This seems more like a general design issue. If you truly want it done that way you could use a timer. But a simple redesign may be more helpful.

If you only want the dialog to be shown when an error occurs why not only show it only then?

Return_Object = new ReturnObject(ReturnCode::netcon_LoginSuccess, QString(""));
if (Return_Object->getCode() != ReturnCode::netcon_LoginSuccess) 
{
    dlgConnectStatus = new LoginStatusDialog(Return_Object);
    dlgConnectStatus->setModal(true);
    int res = dlgConnectStatus->exec();
    //...
}

This way, the dialog who is responsible for only showing the information will only do this. Show the login error. It just seems like a design flaw for creating a dialog then immediately closing it (especially considering that the good case should be your default case).

If you have extra things the Dialog is doing you should consider how much of that code really needs to be in a class intended to be for showing information.

Calling close from constructor is wrong, cause your dialog is showing after constructor ends, so your check can't close dialog, that wasn't showed. You need to call close after(in) QDialog::showEvent . Or, just as I say in your first theme - you can use timer loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM