0

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?

4

1 回答 1

1

您必须使用ignore()on 事件“中止它” - 让 Qt 知道您不希望小部件实际关闭。

如果事件的接收者同意关闭小部件,isAccepted() 函数将返回 true;调用accept() 同意关闭小部件,如果此事件的接收者不希望小部件关闭,则调用ignore()。

此外,无需调用close()自己 - “X”按钮已经这样做了,这就是您收到关闭事件的原因!

所以你的代码应该是:

void MainWindow::closeEvent(QCloseEvent* event)
{
    // accept close event if are not aborted
    event->setAccepted(!isAbortedFilestoSave());
}
于 2015-11-10T09:00:42.757 回答