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 に答える