2

我正在尝试根据哪个选项卡处于活动状态重新分配触发不同事件的键盘按钮。

下面是我的初始化:

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 循环来根据当前的选项卡分配键吗?

4

1 回答 1

1

您的代码不起作用的原因是您并没有真正删除任何内容。该actions()方法将返回一个新的 Python 列表,其中包含对操作的引用。您的循环将清除该 Python 列表,但不会触及内部 Qt 列表,因此这些操作不会被删除。

你想要的,更像是这样的:

    for action in self.actions():
        self.removeAction(action)
        action.deleteLater()
于 2014-04-08T03:02:06.737 回答