Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt application cancel exit event

Tags:

c++

qt

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,

like image 545
Professor Chaos Avatar asked Dec 19 '11 19:12

Professor Chaos


1 Answers

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();
    }
}
like image 181
Karlson Avatar answered Nov 06 '22 04:11

Karlson