Qt.AltModifier和OS X.

时间:2016-02-13 05:12:36

标签: python qt pyqt

问题:在笔记本电脑和OS X上运行时,此代码不会在Qt.AltModifier上打印。它与PyQt和PySide都是一致的。有没有办法检查QKeyEvent == ControlKey?

from PyQt4 import QtCore, QtGui


class Custom(QtGui.QWidget):
    def __init__(self, *args, **kwargs):
        QtGui.QWidget.__init__(self, *args, **kwargs)

    def keyPressEvent(self, event):

        if event.key()==QtCore.Qt.Key_A:
            print 'QtCore.Qt.Key_A'


        if event.key()==QtCore.Qt.ALT:
            print 'QtCore.Qt.ALT'

        if event.key()==QtCore.Qt.AltModifier:
            print 'AltModifier'


        if event.key()==QtCore.Qt.CTRL+QtCore.Qt.Key_A:
            print 'QtCore.Qt.CTRL+QtCore.Qt.Key_B'

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Custom()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

要检查ALT按钮是否已被按下作为修饰符,您需要检查modifiers(),即:

if event.modifiers() == QtCore.Qt.AltModifier:
    if event.key() == QtCore.Qt.Key_A:
        print 'ALT+A'