QT:禁用退出mac应用程序的键盘快捷键或识别只允许关闭按钮事件退出应用程序



在Mac中,Command+Q键盘快捷键退出了大多数应用程序。

在我的情况下,我想为我在QT中开发的应用程序禁用它。

或者有什么方法可以通过单击应用程序窗口的关闭按钮来识别引发的关闭事件吗?如果我能识别它,那么我将不会通过覆盖QT中的关闭函数来退出其余的关闭事件调用。

当然,在任何人的帮助下,我都能找到一盏灯去我想去的地方。

扩展QtGui.QApplication::events()方法以接收此(命令+q)Mac OSX键盘快捷键关闭事件并忽略它。

下面是我的示例pyqt(实现它的代码。

#! /usr/bin/python 
import sys 
import os
from PyQt4 import QtGui 
class Notepad(QtGui.QMainWindow):
    def __init__(self):
        super(Notepad, self).__init__()
        self.initUI()
    def initUI(self):
        self.setGeometry(300,300,300,300)
        self.setWindowTitle('Notepad')
        self.show()
        self.raise_()
    #def keyPressEvent(self, keyEvent):
    #    print(keyEvent,'hi')
    #    print('close 0', keyEvent.InputMethod)
    #    if keyEvent.key() != 16777249:
    #        super().keyPressEvent(keyEvent)
    #    else:
    #        print(dir(keyEvent))
    #        return False
    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()
def main():
    app = Application()
    notepad = Notepad()
    sys.exit(app.exec_())
class Application(QtGui.QApplication):
    def event(self, event):
        # Ignore command + q close app keyboard shortcut event in mac
        if event.type() == QtCore.QEvent.Close and event.spontaneous():
            if sys.platform.startswith('darwin'):
                event.ignore()
                return False

感谢大家

最新更新