0

I know that qdialog->exec() only returns if accept() or reject() is called. However I am using the following code:

    mydialog* p =new mydialog(this);
    p->setWindowModality(Qt::WindowModal); 
    int return_code= p->exec() ;//Block 
    if(return_code==1)
    {
      //Called when `accept()` is called --->Line A
    }
    else
    {
      //Called when reject is called ---> Line B
    }

Now this method is called in mydialog.

void mydialog::someMethod()
{

    if(somecondition)
    {
        dialog_a->setWindowModality(Qt::WindowModal);
        dialog_a->setFixedSize(dialog_a->size());
        this->setVisible(false);

                if(dialog_a->exec()==1) 
                {
                   qDebug() << "Dialog A selected";
                }
                else
                {
                    //back button was pressesed
                    if(dialog_a->terminate)
                    {
                        reject();
                    }
                    else
                    {
                        this->setVisible(true); // --->Line C
                    }
                }
    }
    else
    {
       qDebug() << "Something else selected"; 
    }
}

Now here is the problem when the above method is called, it ends up in Line C. After that Line B is called. Why is that? Why is mydialog is exiting? I havent called reject() or accept() anywhere? Any suggestions?

Morix Dev
  • 2,700
  • 1
  • 28
  • 49
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

2

The problem is this line: this->setVisible(false);

If you look at the source code of QDialog, you can see this part:

    // Reimplemented to exit a modal event loop when the dialog is hidden.
    QWidget::setVisible(visible);
    if (d->eventLoop)
        d->eventLoop->exit();

So when mydialog::someMethod() returns (or whatever calls that method returns), your dialog will close.

thuga
  • 12,601
  • 42
  • 52
  • To put it in other words: `exec` **must** return when the dialog is hidden. Otherwise it would **never** return, since you can't interact with in invisible dialog. In other words, what the asker is seeing is the only sensible behavior. – Kuba hasn't forgotten Monica Sep 19 '14 at 16:51