Question

Is there any way to delay proceed of QEvent in Qt?

This pieces of code just show you what I want to do:

QList<QEvent*> waited_events;

...

bool MyQWidget::event(QEvent * event)
{
    if (event->type() == QEvent::KeyPress)
    {
        waited_events.append(event);
        QTimer::singleShot(100, this, SLOT(timer()));  
        return true;
    } else
    return QWidget::event(event);
}

...

void MyQWidget::timer()
{
    if (!waited_actions.isEmpty())
    {
        QEvent* event = waited_events.takeFirst();
        QWidget::event(event);
    }
}

Thanks!

Was it helpful?

Solution

As a general rule you do not want to store QEvents that are triggered by QWidgets.

The code snippet that you have posted is correct if you want to delay processing of a QEvent. However, your code snippet will not do what you want because when this:

void MyQWidget::timer()
{
    if (!waited_actions.isEmpty())
    {
        QEvent* event = waited_events.takeFirst();
        QWidget::event(event);
    }
}

gets called your QEvent has already been deleted, so waited_events will always be empty.

If you need MyQWidget to use this approach you'll have to create a new event (like F. Osterfeld suggested). I'm not 100% sure but it looks like you are just trying to catch a QEvent::KeyPress. If that's the case then the simple way is to have MyQWidget override the void QWidget::keyPressEvent(QKeyEvent* event).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top