How to de-activate command + Q key in pyqt application in Mac OSX?
Question: My PyQt application is closed when I press (command + q) keys in Mac OSX.

(i.e) My App receives close event similar to pressing (Alt +F4) keys in
windows

But How can I disable this type of closing event which is mac native close
keyboard shortcut.

Following is my sample pyqt code for which I want my qmainwindow should not
receive close event.

    
    
    #! /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 = QtGui.QApplication(sys.argv)
        notepad = Notepad()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

???


Answer: Extend the QtGui.QApplication::events() method to receive this command + q
close event and ignore it.

Below is my sample code to achieve it.

    
    
    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
    

Thanks everyone

