Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Keyboard inputs when a Qdialog opened.



  • Hello. I want to take keyboard inputs when a dialog is open. I'm trying this piece of code in dialog class but it doesn't work.

    void Manual_Window::key_press(QKeyEvent* event){
        if(event->key() == Qt::Key_Q){
           qDebug() << "q is pressed ";
         }
    }
    

    Also I tried this function with QTimer

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),this,SLOT(key_press(event)));
    timer->start(100);
    

    But it doens't work too. Any ideas?


  • Lifetime Qt Champion

    Why do you think your function should somehow magically receive the keyPressEvents() ?



  • I read some examples and saw a video on youtube and did it exactly like this. Sorry I'm new with Qt and programming.


  • Lifetime Qt Champion

    @Alex_Spi13
    Hi
    The key_press is normally called via Qt.
    It gets a key from the Os and then calls key_press for the widget that has focus.

    The timer cannot call it as timeout() do not have a QKeyEvent* to give to it.

    Can I ask why you try to do with timer.
    You can fake/generate keys if you want but not sure what your goal is ?



  • I just tried to call every 100ms the key_press function to see if takes the keyboard input. I want to control a robot when I click to a push button and opened a second dialog. I want to press keys and move the robot. So with first function it would be worked? Why it doesn't?


  • Lifetime Qt Champion

    @Alex_Spi13

    Ah sorry my bad
    the real name is
    void Manual_Window::keyPressEvent(QKeyEvent *event)

    it must be named like that to override the keyPressEvent from the base class. (to let you handle it)

    So its a good idea to define it like this
    virtual void keyPressEvent(QKeyEvent *event) override;

    (in the .h)
    as then compiler will warn you if you dont actually override it.

    Also as a note.
    In event based applications. You will almost never go and pull for the keys yourself.
    It will come in the form of an event.
    This is important to understand as that is why the timer don't work, and does not it work that way.



  • Thanks it worked!!!
    It's something like a callback? When I press a key, goes to the function as an input?


  • Lifetime Qt Champion

    @Alex_Spi13
    Hi
    Yes you could say it's a sort of a callback.
    But its also a design called event driven.

    So the QApplication get the keys / mouse / other stuff from the operating system and
    then uses events internally to pass the data around as events.
    All those event handlers are marked as virtual in the Qt Widget classes so you can override them to change what should happen at a given event.

    If you extend a classes that itself uses one of these event handlers, it's important to call that before or after ones one code
    so the orginal features does not break.

    https://doc.qt.io/qt-5/eventsandfilters.html


  • Lifetime Qt Champion

    @Alex_Spi13 said in Keyboard inputs when a Qdialog opened.:

    I read some examples and saw a video on youtube and did it exactly like this.

    No they did not. Or they were wrong. Read the docs (the link I gave you).



  • @mrjj said in Keyboard inputs when a Qdialog opened.:

    All those event handlers are marked as viraul in the Qt Widget classes so you can override them to hange

    Aaa ok thanks now I got it. I was confused because I have made a mini game with SDL and I have to run the code in loop to take the user inputs. Thanks a lot for the help and the useful information. Have a nice day :)


  • Lifetime Qt Champion

    @Alex_Spi13
    Hi
    Ahh. yes SDL is completely the opposite in regards to input as there one has to read it in a loop as you say.
    Nice day to you too :)
    Please flag as solved using the Topic button in first post.


Log in to reply