Qt dialog how can I trigger accept() and reject() from function

user63898 picture user63898 · May 26, 2011 · Viewed 9k times · Source

I have situation that I open QDialog window from the main.cpp file and then I wait for the exec() method to return based on success or fail of the QDialog. Like this :

   int main( ... ) {
    LoginDialog *loginDlg = new LoginDlg;

    if( loginDlg->exec() != Qt:;Accepted ) {
    return 0;
    }

    //check the login Info
    delete loginDlg;

    MainWindow w;
    w.show()
    return app.exec();
    }

From the Qt Examples (Address book) I saw I just can use the accept() and reject() slots. The thing is that I like the window to close based on some function flow, and not ok/close buttons. How can I trigger those slots from function? .

Answer

Fivos Vilanakis picture Fivos Vilanakis · May 26, 2011

As liaK pointed out you can just call the following functions from your code:

loginDlg->accept();
loginDlg->reject();

You can also call the following equivalent function using the result as a parameter:

loginDlg->done(QDialog::Accepted);
loginDlg->done(QDialog::Rejected);

PS: Note also there is no Qt::Accepted value as specified in your question. The correct constant is QDialog::Accepted