How can I disable Alt + F4 window closing using Qt?

99封情书 提交于 2019-12-02 20:53:33

Pressing Alt+F4 results in a close event being sent to your top level window. In your window class, you can override closeEvent() to ignore it and prevent your application from closing.

void MainWindow::closeEvent(QCloseEvent * event)
{
    event->ignore();
}

If you left the close button (X) visible, this method would also disable it from closing your app.

This is usually used to give the application a chance to decide if it wants to close or not or ask the user by displaying an "Are you sure?" message box.

Alexander Chernin

The code below prevents a dialog close when pressed Alt+F4, [X] or Escape, but not by calling SomeDialog::close() method.

void SomeDialog::closeEvent(QCloseEvent *evt) {
    evt->setAccepted( !evt->spontaneous() );
}   

void SomeDialog::keyPressEvent(QKeyEvent *evt) {
    // must be overridden but empty if the only you need is to prevent closing by Escape
}   

good luck to all of us ;)

Also you can handle the event in your dialog's class (at least if it's modal dlg):

void MyDialog::closeEvent(QCloseEvent* e)
{
    if ( condition )
       e->ignore();
    else
       __super::closeEvent(e);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!