Important: Please read the Qt Code of Conduct -

SetModal() does not work in QDialog

  • For some reason the setModal method is not working for me. Below is a snippet of code that calls a QDialog.

    @QHash<QString, QVariant>* DataAccess::getSettings() // Private method
    QHash<QString, QVariant> *parameters = new QHash<QString, QVariant>();

    Configconnec *confconnec; /GUI for get parameters of connection/

    QSettings connecsettings( QSettings::IniFormat, QSettings::SystemScope, "Freedom", "TecTracker");


    if(!connecsettings.contains("host"))    /*If connection settings not existis*/
        confconnec =  new Configconnec();
        confconnec->exec&#40;true&#41;;   /*Get new connection settings*/
    parameters->insert("host", connecsettings.value("host", ""));
    parameters->insert("port", connecsettings.value("port", 5432));
    parameters->insert("database", connecsettings.value("database", "postgres"));
    parameters->insert("user", connecsettings.value("user", "postgres"));
    parameters->insert("passw", connecsettings.value("passw", "postgres"));


    delete confconnec;

    return parameters;

    exec() method of Confconnec

    @QDialog* Configconnec::exec(bool modal) // Show connection form
    return diagconnec;

    If diagconnec is not modal, the program don't stop in line:

    @ if(!connecsettings.contains("host")) /If connection settings not existis/
    confconnec = new Configconnec();
    confconnec->exec(true); /DON'T STOP HERE/

    He leaves the IF and step through the code, and I do not want it. He should expect the user closes the Dialog to proceed.
    I get the desired effect by calling the exec(), but with setModal() and show() does not work.

  • Lifetime Qt Champion


    There's no reason for your dialog to stop your application there. In your custom exec you are calling show which purpose is exactly to show your dialog without blocking the event loop.

  • Strange, the "documentation": says that if we define the modal property to true, the show() loads widget as modal. And I'm setting to true before calling show()

  • Lifetime Qt Champion

    AFAIK, modal doesn't mean "blocking your application's event loop", it means that you are blocking the access to the widgets underneath. If you want to block your application's flow, call the original exec()

  • ok!

    exec() is the only option or is there some property that can be set?

  • Lifetime Qt Champion

    It's the only option, however take also a look at open, it's also not blocking but proposes an alternative architecture that's worth to think about.

Log in to reply