views:

615

answers:

3

How can I ignore all mouse and keyboard events and later dont ignore in QT. It is, click a button, ignore all events in childs, click again not ignore. is it clear? I have next lines, but maybe I use in wrong way:

setAttribute(Qt::WA_TransparentForMouseEvents);

setFocusPolicy( Qt::NoFocus );

thanks,

+1  A: 

You could use:

QWidget::setEnabled(false)

it disable mouse and keyboard events for a widget.

Massimo Fazzolari
+1  A: 

Hey,

You can use 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 !

Andy M
+1  A: 

Hi,

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 ! `

Matthieu