python pyqt4 closeEvent 阻止 Windows 重新启动/关闭?
我在应用程序中添加了 closeEvent,以防止用户按 alt-f4 并关闭应用程序。我现在的问题是,当我在开始菜单中手动关闭或重新启动计算机时,没有任何反应。
def closeEvent(self, event):
event.ignore()
我也尝试了这段代码,但它没有继续执行 if TRUE 语句。
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_F4 and (event.modifiers() &
QtCore.Qt.AltModifier):
print 'do something'
i added closeEvent in my application to prevent the user from pressing alt-f4 and closing the application.My problem now is when i shutdown or restart the computer manually in the start menu, nothing happens.
def closeEvent(self, event):
event.ignore()
i also try this code but it didn't proceed to the if TRUE statement.
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_F4 and (event.modifiers() &
QtCore.Qt.AltModifier):
print 'do something'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用QApplication.setQuitOnLastWindowClosed吗?
当设置为
False
时,这将使应用程序在顶级窗口关闭时保持活动状态。Could you make use of QApplication.setQuitOnLastWindowClosed?
When set to
False
, this will keep the application alive when the top-level window is closed.我不确定你是否想阻止系统关闭,或者允许它关闭,但你应该看看
QApplication.commitData
,它通常在系统关闭时调用,例如,您可以在 commitData 中设置一个标志,以帮助区分您应该忽略的正常关闭事件和由
closeEvent
函数中的系统引起的事件。I'm not sure if you want to prevent the system from shutting down, or allow it to shutdown, but you should have a look at
QApplication.commitData
which is normally called when the system is shutting down, andQSessionManager
You could for example set a flag in commitData to help differentiate between a normal close event that you should ignore and one that is due to the system in the
closeEvent
function.