0
votes

I'm using PySide with Qt 4.7 and having a problem with keyReleaseEvent in my QMainWindow. Here is the code:

def keyReleaseEvent(self, event):
    if event.key() == QtCore.Qt.Key_Alt:
        print 'Alt Key Released'
        if self.pointerTypeGroup.checkedId() != self.scene.HandDrag:
            print "Disabling Drag"
            self.pointerGroupClicked(self.pointerTypeGroup.checkedId())
            #event.accept()
    #super(MainWindow, self).keyPressEvent(event)

This only works on every other key release. The first is ignored, the second works, the third fails, the fourth works, and so on. It doesn't matter whether I implement keyPressEvent, whether I put in an event.accept() or whether I call the base implementation afterwards. Those have no effect on the behaviour.

I'm using this to make a map in a QGraphicsView draggable. If I implement keyPressEvent and drag the map, as long as I manipulate the map between each key press, keyReleaseEvent is never called.

Any ideas what's going on?

1

1 Answers

0
votes

It rings bells to me that the Alt key is a modifier key, which is to be identified by the event.modifiers() method. Check this:

def keyPressEvent(self, event):
    if event.key() == QtCore.Qt.Key_O and ( event.modifiers() & QtCore.Qt.ALT ):
        print 'yeah' # Or your actual action code

For reference, check the QtCore.Qt namespace reference. Hope this helps!

I have been guided by this blog post