Important: Please read the Qt Code of Conduct -

Problem with QKeyEvent

  • Hello,

    I design an application where I will not have access to the system keyboard. It is for this key panel monitor connected via RS232. The buttons have to simulate the keys Tab, Backtab, arrows and Enter.
    In the class of the main dialog , I emit signals corresponding buttons, ie.

    if (key == KEY_TAB)
        emit KeyPressTab ();

    This signal is captured in the various dialog boxes by using the call:

    QObject :: connect (mainwindow, SIGNAL (KeyPressTab ()), this, SLOT (OnKeyPressTab ()));

    In turn, the same function slot is:

    void SecondDialog :: OnKeyPressTab ()
        QKeyEvent event (QEvent :: KeyPress, Qt :: Key_Tab, Qt :: NoModifier);
        QApplication :: sendEvent (this, & event);

    In this way, tabulation and BackTab works without a problem, but the arrow and enter not work.
    I am using the same mechanism, such as:

    ------------------------------- ----------------- Main Window ------------
    if (key == KEY_TAB)
        emit KeyPressLeft ();
    ------------------------------- ----------------- Second Dialog-----------
    QObject :: connect (mainwindow, SIGNAL (KeyPressLeft ()), this, SLOT (OnKeyPressLeft ()));
    void SecondDialog:: OnKeyPressLeft ()
        QKeyEvent event (QEvent :: KeyPress, Qt :: KEY_LEFT, Qt :: NoModifier);
        QApplication :: sendEvent (this, & event);

    Unfortunately, no reaction.
    Please help.

  • Moderators

    how/where do you check the key event?
    If you are doing it in the dialog base-class i guess the focus widget is "swallowing/consuming the key event and thus it's not propagated up to the dialog.

    You should use qApp->installEventFilter() and filter the key events there (maybe check if the receiver QObject is inside your dialog - before they are send to the focus widget.

  • The events pressing the keys of the external keyboard are handled in a separate process (writing under Debian). In the main class of the program Qt, I have a receiving thread and using mutex_trylock and mutex_unclock. Receiving data drom an external keyboard works correctly.

    I am surprised that the keys Tab and Backtab work properly and the other did not respond.

    I'll try to use the filtering mechanism of events with


    For clarification I posted algorithm below.

Log in to reply