user266617
user266617

Reputation:

Ignore mouse and keyboard events in Qt

In Qt, how can I ignore all mouse and keyboard events and later stop ignoring them? That is: click a button, ignore all events in children; click again, not ignore. Is that clear? I have the following lines, but maybe I'm doing something wrong:

setAttribute(Qt::WA_TransparentForMouseEvents);

setFocusPolicy(Qt::NoFocus);

Upvotes: 12

Views: 33003

Answers (4)

Andy M
Andy M

Reputation: 6065

You can use Events' filters on your mouse and keyboard events to filter some keypress or mouseclick when you need so :

yourWidget->installEventFilter(this);

...

bool YourFrm::eventFilter(QObject* pObject, QEvent* pEvent)
{
    if (pEvent->type() == QEvent::KeyPress) 
    {
        QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(pEvent);
        int PressedKey = pKeyEvent->key();

        if(PressedKey == Qt::Key_Return)
        {
            // Filter Return key....
            return true;
        }

        // standard event processing
        return QObject::eventFilter(pObject, pEvent);
    }
    else if (pEvent->type() == QEvent::MouseButtonPress) 
    {
        QMouseEvent* pMouseEvent = static_cast<QMouseEvent*>(pEvent);

        ... // etc...
    }
    else 
    {
        // standard event processing
        return QObject::eventFilter(pObject, pEvent);
    }
}

More informations on this : http://qt.nokia.com/doc/4.6/eventsandfilters.html

Hope it helps !

Upvotes: 9

danjy
danjy

Reputation: 141

Dont use setFocusPolicy(Qt::NoFocus); and it will propagate events to the parent. Use only setAttribute(Qt::WA_TransparentForMouseEvents);

Upvotes: 14

Matthieu
Matthieu

Reputation: 2761

Do you mean for a QGraphicsItem ?

If yes, you can call

void QGraphicsItem::setEnabled ( bool enabled )

And to activate the event later, as the item doesn't receive events any more, you have to pass by the Scene, because you can't receive directly event on the item.
If your problem is not using GraphicsView Frameworks, but other part of qt, it's almost the same process :
You can call :

QWidget::setEnabled(false) //like Massimo said

In order to reactive the widget, just detect press event inside an object in your application to be able to call `setEnable(true) on your widget !

Hope it helps ! `

Upvotes: 5

Massimo Fazzolari
Massimo Fazzolari

Reputation: 5202

You could use:

QWidget::setEnabled(false)

it disable mouse and keyboard events for a widget.

Upvotes: 5

Related Questions