Redirecting mouse events
-
Hi. I'm a relative newcomer to Qt, and am having problems re-directing events to a map of pushbuttons. The application I'm working on is a touch-screen application. On the lower part of the application is an series of pushbuttons. Part of our requirement is that the active area of the pushbutton is greater than the visible area (for particularly fat fingers!). I couldn't think of a more elegant solution than simply installing an eventHandler (on the application), determining the X-Y coordinates of the event, and if the event is within a certain distance of the button, re-directing it to the button itself.
I try to re-direct the event to the widget by calling QObject::eventFilter, and passing the pushbutton and the event, like this:
// selectedMimicWidget is a QPushButton. The original event may have been directed to this widget, or just outside it.
QObject::eventFilter( &( selectedMimicWidget ), qEvent );
But it doesn't seem to work. Any thoughts anyone?
-
you must install an event filter for your application if you want it to be used like an event filter. However another solution would be to reimplement mousePressEvent of your mainWindow, check the position for your event with event->pos() and send the event to your corresponding pushbutton manually. On a side note, try to put your code in between @ for better reading
-
Thanks b1gsnak3 - I have installed the event filter. It works insofar as it intercepts the correct mouse events, I use the position to retrieve the QPushButton to which to re-direct the event
But calling eventFilter, substituting the PushButton for the widget which was the original target of the event doesn't seem to work.
What call should I use to re-direct the event? I tried using QApplication::sendEvent(), but doesn't that result in recursion?
-
My situation wasn't quite the same, but has some similarities... I have a widget (ui->historyTextEdit) that responds to only certain key press events, and any other typing should change focus to another widget (ui->commandLineTextEdit) which will then handle the keyboard. But that first key press event gets eaten by the original widget, so I needed to forward it to the second widget.
Simply re-sending it didn't work, I think because the event objects are destroyed after being removed from the event queue.
What worked for me was to make a new event that is a copy of the key event, then post that event to the second widget.
@switch (theEvent->type()) {
case QEvent::KeyPress:
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(theEvent);
switch (keyEvent->key())
{
case Qt::Key_Return:
... a few other cases ...
default:
if (target == ui->historyTextEdit && keyEvent->modifiers()==0)
{
ui->commandLineTextEdit->setFocus(Qt::OtherFocusReason);
QKeyEvent * newEvent = new QKeyEvent(*keyEvent);
QApplication::postEvent(ui->commandLineTextEdit, newEvent);
return true;
}
} // end switch (keyEvent->key())
} // end case QEvent::KeyPress:
break;
@
Hope this helps. -
Thanks John. My problem is that the array of buttons overlays other widgets, so the event handler is installed on the Application. Any attempt to post the event results in recursion. I may have to re-design it so the event handler is not on the application, but on the others widgets themselves.
Thanks for your help!