Ralf Wickum
Ralf Wickum

Reputation: 3274

Abort a QCloseEvent

In my application, I am handling a QCloseEvent (when the close button X was pressed):

void MainWindow::closeEvent(QCloseEvent* event)
{
    if ( !isAbortedFilestoSave() ) {
      this->close();
    }
    // else abort
}

The if clause is triggereed when no abort was pressed. I would like to implement an else clause where a QCloseEvent is aborted? How?

Upvotes: 0

Views: 774

Answers (1)

user362515
user362515

Reputation: 917

You must use the ignore() on the event to "abort it" - to let Qt know you don't want the widget to actually close.

The isAccepted() function returns true if the event's receiver has agreed to close the widget; call accept() to agree to close the widget and call ignore() if the receiver of this event does not want the widget to be closed.

Also, no need to call close() yourself - the "X" button does that already, that's why you receive a close event!

So your code should be:

void MainWindow::closeEvent(QCloseEvent* event)
{
    // accept close event if are not aborted
    event->setAccepted(!isAbortedFilestoSave());
}

Upvotes: 1

Related Questions