0

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

就我而言,我想为我在 QT 中开发的应用程序禁用它。

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

毫无疑问,在任何人的帮助下,我都能找到一盏灯去我想去的地方。

4

1 回答 1

0

扩展 QtGui.QApplication::events() 方法来接收这个 (command + 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

感谢大家

于 2013-08-01T11:36:15.063 回答