Important: Please read the Qt Code of Conduct -

How to Run an event (keypressevent) in the background ?

  • I need to be checking the keyboard input for a bar-code-reader (this machine works like a keyboard, typing the code and an "enter" at the end). But I can't keep the parent widget always in front. So, how can I keep the keyPressEvent running?

    I found out that QEventLoop may helps, but I don't know how to use it in my constructor. There are not many examples in the Internet.

    Thanks for your help.

    @class monitor : public QWidget

    explicit monitor(QWidget *parent = 0);
    char code[8];
    void keyPressEvent(QKeyEvent *e);
    QEventLoop loop;

    Ui::monitor *ui;

  • Moderators



    (You don't create your own event loop in your class. QApplication::exec() creates the event loop in the main() function)

  • Hi,

    first, the title and the content are misleading somehow as they suggest different things: the title request for a "run" as a code generated key press event and the solution is as JKSH hinted, and the content request for a silent key input checking solution :)

    for checking the keyboard input silently I use to grab the keyboard input(by grabKeyboard() method ) to a defined widget(QPushButton or whatever) - by this you avoid to check for any sender , and install an event filter for it to fetch key press events

    hope this helps!


Log in to reply