您所需要做的就是在您的按钮处理程序中检查
QApplication.keyboardModifiers,并根据需要选择其他操作:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ShiftModifier:
print('Shift+Click')
elif modifiers == QtCore.Qt.ControlModifier:
print('Control+Click')
elif modifiers == (QtCore.Qt.ControlModifier |
QtCore.Qt.ShiftModifier):
print('Control+Shift+Click')
else:
print('Click')
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
(注意:various modifiers可以OR’d在一起,以便检查多键组合).