I have a Qt
application with multiple widgets showing buttons at the same time. In certain circumstances, I want key presses to be sent to one of the widgets, even if that widget doesn't have focus. To do this, I have overridden keyPressEvent()
in the master widget (that owns all subwidgets in this application) and re-send the key event to the subwidget if it doesn't have focus using code similar to this:
if (!someWidget->hasFocus())
{
QApplication::sendEvent(someWidget, keyEvent);
}
This works great as long as someWidget
handes said event. If it ignores it, then it enters a nasty infinite recursive loop since events flow up to parents.
Is there a way to know where an event came from so I can prevent this infinite loop? I'm thinking of something like this:
if (!someWidget->hasFocus() && (keyEvent->source != someWidget))
{
QApplication::sendEvent(someWidget, keyEvent);
}
Or is there a different way I can prevent this from happening?