event filter receives an event of type QEvent::KeyPress but the event has no key() method

edited July 2015 in General

I put an event filter into my Ruby program. It gets triggered by the correct interface
and correctly identifies key press events, but I cannot identify what key has been pressed.
It looks like I have a generic event that does not know it's a key, so a call to event.key()
is undefined. In C++ one would typecast the event to a key event, but I don't know of the
ruby equivalent and suspect there's another way.

class KeyPressEater < QObject
    def eventFilter(obj, evt)
        if evt.type == QEvent::KeyPress
            if evt.key == Qt::Key_Tab  # dies here: undefined method 'key'
                obj.keyPressEvent(evt)
            end
        else
            super
        end
    end
end

Anyone know how I identify what key has been pressed?

Thanks,
Dave

Comments

  • edited July 2015

    Try 0x01000001 in place of Qt::Key_Tab, which is the actual hex value that should be inside Key_Tab (link). If that doesn't work, can you post a runnable script? i.e. a script where you invoke KeyPressEater. That will help debug.

    David

  • edited November -1

    Hi David,

    are you using 0.24 Python eval? There was an issue with subclasses passed to arguments of basic class type. This will hit here. It has been fixed in 0.24, but it may be there in 0.23 as well.

    In fact, "obj" is not a QKeyEvent as expected, but is a plain "QEvent". As such, it does not have a key method. And sadly, there is no way of working around that in the event filter without that fix.

    Matthias

  • edited July 2015

    Matthias,

    Thank you. I am using 0.24 Python eval because it was suggested I move from 0.23. The goal was to get better behavior from enums that seemed to be missing or inaccessible in 0.23 as arguments of ruby calls to Qt.

    I am getting exactly the behavior you described, that is, a QEvent rather than a QKeyEvent. So it seemed that I was supposed to somehow cast it to the correct event type, but from what you say, I just need a code fix.

    I'll try 0.24 when I have a chance. The most recent build, r2847, still identifies itself in version.h as
    0.24-python-eval. Is there a way for me to get the code fix?

    Dave

Sign In or Register to comment.