Professor Chaos
Professor Chaos

Reputation: 9060

Qt application cancel exit event

I was wondering if it is possible to ignore/cancel exiting the application based of certain boolean flag is set even if the user were to click the red "X" (close window button).

I'm a c# programmer and I know it's pretty easy to do for .net applications but I'm fairly new to qt framework and searching on google did not fetch any relevant results.

Thanks,

Upvotes: 10

Views: 12884

Answers (1)

Karlson
Karlson

Reputation: 3038

Qt's documentation describes this specific use-case about asking permission to close in their examples.

If you subclass QMainWindow for example and reimplement the closeEvent function you can provide your app with customized behavior when someone tries to close it. For example:

void MainWindow::closeEvent(QCloseEvent *event)
{
    if (maybeSave()) {
        writeSettings();
        event->accept();
    } else {
        event->ignore();
    }
}

Upvotes: 19

Related Questions