我尝试根据哪个标签处于活动状态来重新分配触发不同事件的键盘按钮。
以下是我的初始化:
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" to self.pushButtonSelectLeft and key" 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循环可以根据哪个标签是最新的来分配键?
答案 0 :(得分:1)
您的代码不起作用的原因是您并没有真正删除任何内容。 actions()
方法将返回包含对操作的引用的新Python列表。你的循环将清除Python列表,但它不会触及内部Qt列表,因此操作不会被删除。
你想要什么,更像是这样:
for action in self.actions():
self.removeAction(action)
action.deleteLater()