Question

I'm trying to reassign keyboard buttons that trigger different events based on which tab is active.

Below is my initialization:

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)  

So when I start up my application, these are the original key commands. If I try to set key "a" to self.pushButtonSelectLeft and key "d" to self.pushButtonSelectRight which reside on a different tab, nothing happens and the original WASD commands persist.

I've tried to delete the list of actions and re-initialize the list of actions but that just completely deletes all the actions and no key presses work:

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....

Is there some sort of if-elif loop that I could write to assign the keys based on which tab is current?

Was it helpful?

Solution

The reason your code doesn't work, is that you are not really deleting anything. The actions() method will return a new Python list containing references to the actions. Your loop will clear that Python list, but it won't touch the internal Qt list, and so the actions won't get deleted.

What you want, is something more like this:

    for action in self.actions():
        self.removeAction(action)
        action.deleteLater()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top