Send KeyEvent to hidden Object

  • Hi

    I need to send KeyEvents to hidden objects.
    For shown objects, this works perfectly when I set the focus (setFocus()) to the object that should receive the event and then the key event is distributed using the normal event handler - works as expected.
    According documentation, this is not possible for hidden objects, and that's exactly what I stumbled across. (Doc 4.6: Be aware that if the widget is hidden, it will not accept focus until it is shown.)

    Any idea how I can make a hidden object accepting the keyEvent?


  • Moderators

    simply you don't, since this is given by design.

    But what are you trying to achieve anyway? Why does a hidden widget need to receive an input event?!
    This sounds like a design issue in your code.

  • We are using multiple webView's to create a very fast and responsive GUI for an embedded system. The GUI's are preloaded and are hidden/shown.
    There are cases where a currently still hidden GUI should receive a keyevent before it is shown, so that it is already prepared before the show applies.
    Of course, if i show it first and then send the key, this works. However, it does not look nice.

  • Moderators

    how are you exactly sending the key events?
    Because actually hidden widgets can also receive key events. The focus is just necessary for the application to determine to which widget the input event should be send.

    Unless the WebView has some internal check if it's visible or if it already has been rendered etc.

    But install an event filter on the hidden webview widget and check if the key event is received.

    But as i said it should work (at least until to this point)

  • For shown objects, the Focus is set to the respective obj and then
    the key event (generated from a serial input) is sent like this:

    QKeyEvent keyPress(QEvent::KeyPress, SysKeyCode, Qt::NoModifier);
    QApplication::sendEvent(ActWidget, &keyPress);
    QKeyEvent keyRelease(QEvent::KeyRelease, SysKeyCode, Qt::NoModifier);
    QApplication::sendEvent(ActWidget, &keyRelease); }

    Is there a way to post the key event directly to an object instead of sending it to the application event handler?

  • Moderators

    yes and this works also for hidden widgets. As i said, give it a try installing an event filter on that widget.
    It may be that the webview widget internally does some additional checking if it is visible idk.

  • OK ... I'm not sure where to add this event filter.
    I have an event filter in class MyApplication where I do some mouse/touch handling and forward unhandled events to QApplication.

    The webView's I use are not subclassed, I'm creating them as needed in code at runtime in my MainWindow class.

    Sorry for my missing knowledge ... where do I add this eventfilter for a specifc webView?

  • Moderators

    @McLion said in Send KeyEvent to hidden Object:

    where do I add this eventfilter for a specifc webView?

    on the webview widget (QObject::installEventFilter())

  • I now have an eventfilter on every of my (16) webView.
    I added some debug on the MyApp notify and in the new eventfilter.
    I can see that myApp gets every keypress (first) and the installed eventfilter on the webView's gets the keypress only if any of the webView is shown.

    I'm not sure how to proceed or how this helps in forwarding the event to a specific webView since the event is not getting to the eventfilter if a webView is not active.

    How about catching the event in MyApp notify and somehow posting it to a specific webView?

    Thanks for your help.