QShortCut
只接受QtCore.Qt::KeyboardModifiers的组合。尝试使用事件过滤器:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QTextEdit):
modeChanged = QtCore.pyqtSignal(bool)
_seenMinus = False
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.installEventFilter(self)
self.modeChanged.connect(self.on_modeChanged)
def on_modeChanged(self):
print "Changing Mode."
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Minus:
self._seenMinus = True
elif event.key() == QtCore.Qt.Key_Greater \
and event.modifiers() == QtCore.Qt.ShiftModifier \
and self._seenMinus:
self.modeChanged.emit(True)
self.setStyleSheet("""
background-color: lightgray;
""")
elif event.modifiers() != QtCore.Qt.ShiftModifier:
if self._seenMinus == True:
self.modeChanged.emit(False)
self._seenMinus = False
self.setStyleSheet("")
return super(MyWindow, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())