Qt How to Filter keyPressEvent with Autorepeat

linsek Source

I'm trying to filter key press/release events so that I can setup timers for long press events for my application (e.g. pop up a menu when the down error is held for 3 seconds). I'm having some trouble with some inconsistency from autorepeat of QKeyEvent though. Here is my code:

Mainview::Mainview (QWidget *parent) :
    QMainWindow (parent)
{
    setupUi (this);
    qApp->installEventFilter(this); // event filter for key presses
    ...
}

void Mainview::ControlsPressEvent(QKeyEvent *event)
{
    if ((event->isAutoRepeat()))
    {
        return;
    }

    QString s = QString("Key PRESS event: %1.").arg(event->text());
    qDebug() << s;
}

void Mainview::ControlsReleaseEvent(QKeyEvent *event)
{
    if ((event->isAutoRepeat()))
    {
        return;
    }

    QString s = QString("Key RELEASE event: %1.").arg(event->text());
    qDebug() << s;
}

bool Mainview::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::KeyPress)
    {
        qDebug() << event << " | " << obj;
        /* Only grab the key event from the parent window object */
        if (obj->objectName() == "MainviewClassWindow")
        {
            this->ControlsPressEvent(dynamic_cast<QKeyEvent*>(event));
            return true;
        }
    }
    else if (event->type() == QEvent::KeyRelease)
    {
        qDebug() << event << " | " << obj;
        /* Only grab the key event from the parent window object */
        if (obj->objectName() == "MainviewClassWindow")
        {
            this->ControlsReleaseEvent(dynamic_cast<QKeyEvent*>(event));
            return true;
        }
    }
    else
    {
        return QObject::eventFilter(obj, event);
    }

    return false;
}

Sometimes it works and sometimes it doesn't:

Here is qDebug output in a working example where I press the key once, hold, and release:

"Key PRESS event: ."
QKeyEvent(KeyRelease, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
QKeyEvent(KeyPress, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
QKeyEvent(KeyRelease, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
QKeyEvent(KeyPress, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
QKeyEvent(KeyRelease, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
QKeyEvent(KeyPress, Key_Down, autorepeat, count=1)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key RELEASE event: ."

Here is qDebug output in a non-working example where I press the key once, hold, and release:

QKeyEvent(KeyPress, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key PRESS event: ."
QKeyEvent(KeyRelease, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key RELEASE event: ."
QKeyEvent(KeyPress, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key PRESS event: ."
QKeyEvent(KeyRelease, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key RELEASE event: ."
QKeyEvent(KeyPress, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key PRESS event: ."
QKeyEvent(KeyRelease, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key RELEASE event: ."
QKeyEvent(KeyPress, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key PRESS event: ."
QKeyEvent(KeyRelease, Key_Down)  |  QWidgetWindow(0xa522a0, name = "MainviewClassWindow")
"Key RELEASE event: ."

Why is the autorepeat member of QKeyEvent inconsistent in being set? Maybe this is the incorrect approach to key press, hold, and release processing?

Thanks

qteventskeypresseventfilterautorepeat

Answers

comments powered by Disqus