Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Keyboard inputs when a Qdialog opened.
Forum Updated to NodeBB v4.3 + New Features

Keyboard inputs when a Qdialog opened.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 833 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex_Spi13
    wrote on last edited by Alex_Spi13
    #1

    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?

    1 Reply Last reply
    1
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Alex_Spi13
        wrote on last edited by
        #3

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

        mrjjM Christian EhrlicherC 2 Replies Last reply
        1
        • A Alex_Spi13

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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 ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Alex_Spi13
            wrote on last edited by
            #5

            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?

            mrjjM 1 Reply Last reply
            0
            • A Alex_Spi13

              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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @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.

              1 Reply Last reply
              3
              • A Offline
                A Offline
                Alex_Spi13
                wrote on last edited by
                #7

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

                mrjjM 1 Reply Last reply
                0
                • A Alex_Spi13

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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

                  1 Reply Last reply
                  1
                  • A Alex_Spi13

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

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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).

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Alex_Spi13
                      wrote on last edited by
                      #10

                      @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 :)

                      mrjjM 1 Reply Last reply
                      0
                      • A Alex_Spi13

                        @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 :)

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        @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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved