简体   繁体   中英

How is Qdialog.exec() returning

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?

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.

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