如何使用Qt禁用Alt + F4窗口关闭? [英] How can I disable Alt + F4 window closing using Qt?
问题描述
我已使用以下行从对话框中禁用了Qt中的X按钮:
I've disabled X button in Qt from my dialog using this line:
myDialog->setWindowFlags(Qt::Dialog | Qt::Desktop)
但是我无法使用以下代码检测到 Alt + F4 :
but I couldn't detect Alt + F4 using this code:
void myClass::keyPressEvent(QKeyEvent *e)
{
if ((e->key()==Qt::Key_F4) && (e->modifiers()==Qt::AltModifier))
doSomething();
}
我应该怎么做才能检测 Alt + F4 或在Qt中将其禁用?
what should I do to detect Alt+F4 or disable it in Qt?
推荐答案
按下Alt+F4
会导致将关闭事件发送到顶级窗口.在您的窗口类中,您可以覆盖closeEvent()
来忽略它并阻止您的应用程序关闭.
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();
}
如果使关闭按钮(X)保持可见状态,则此方法还将使它无法关闭应用程序.
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.
这篇关于如何使用Qt禁用Alt + F4窗口关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!