4
votes

I have implemented a sort of gestures listener in the parent widget. If the mouse event is a tap ( press and release with out any move events) then the children of that widget should handle the event, if not, and the events describe a swipe, then the parent widget should handle the events. Is there any way to redirect events to the parents first, then rebroadcast them so that the appropriate child could handle it if the need arises.

1

1 Answers

2
votes

I think http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter is what you are looking for. So what you do is that you set any class inheriting from QObject and set it as the event filter. So to quote from the DOCs:

An event filter is an object that receives all events that are sent to this object. 
The filter can either stop the event or forward it to this object.

The example in the link is for a QKeyEvent but can obviously be adapted to use various mouse functions as well. Below is a dummy example that would 'eat' the first click on a QPushButton while if you double click, the event would go through as normal (note that in this particular example first you would call the 'eat' sequence and upon the second click in a short time the button would take over).

bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{
  if( obj != pushButton ) {
    return QObject::eventFilter(obj, event);
  }
  if (event->type() ==  QEvent::MouseButtonPress ) {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    qDebug("Intercepted mouse click with button %d", mouseEvent->button());
    return true;
  } else {
    // standard event processing                                                                                                                                                                                              
    return QObject::eventFilter(obj, event);
  }
}

Does this help?