我正在尝试根据哪个选项卡处于活动状态重新分配触发不同事件的键盘按钮。
下面是我的初始化:
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("w"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonForward, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("s"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonReverse, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("a"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonLeft, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("d"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonRight, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("Space"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonStop, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
所以当我启动我的应用程序时,这些是原始的键盘命令。如果我尝试将键“a”设置为 self.pushButtonSelectLeft 并将键“d”设置为 self.pushButtonSelectRight,它们位于不同的选项卡上,则不会发生任何事情,并且原始 WASD 命令仍然存在。
我试图删除动作列表并重新初始化动作列表,但这只是完全删除所有动作并且没有按键工作:
if tabIndex == 2:
actionList = self.actions()
for i in actionList:
del i
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("a"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonSelectLeft, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
action = QtGui.QAction(self)
action.setShortcut(QtGui.QKeySequence("d"))
self.connect(action, QtCore.SIGNAL("activated()"), self.pushButtonSelectRight, \
QtCore.SLOT("animateClick()"))
self.addAction(action)
elif tabIndex == 3:
# delete list again
# change "a" to trigger 'Send button'
# change "d" to trigger 'Next button'
elif tabIndex == 4:
# etc....
我可以编写某种 if-elif 循环来根据当前的选项卡分配键吗?