Qt 屏蔽Alt + F4 组合键

Qt 屏蔽Alt + F4 组合键

有时项目中会有特殊要求,比如屏蔽Alt + F4组合键,防止用户关闭窗口。
查看帮助文档,有如下说明:

[virtual protected] void QWidget::closeEvent(QCloseEvent *event)

This event handler is called with the given event when Qt receives a window close request for a top-level widget from the window system.
By default, the event is accepted and the widget is closed. You can reimplement this function to change the way the widget responds to window close requests. For example, you can prevent the window from closing by calling ignore() on all events.
Main window applications typically use reimplementations of this function to check whether the user's work has been saved and ask for permission before closing. For example, the Application Example uses a helper function to determine whether or not to close the window:
  void MainWindow::closeEvent(QCloseEvent *event)
  {
      if (maybeSave()) {
          writeSettings();
          event->accept();
      } else {
          event->ignore();
      }
  }

如果不需要给出任何提示,可以直接忽略事件即可,如下:

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

猜你喜欢

转载自blog.csdn.net/linuxwuj/article/details/78547839
F4