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. How to emit signal when detecting TAB key press?
Forum Updated to NodeBB v4.3 + New Features

How to emit signal when detecting TAB key press?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.6k Views
  • 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.
  • H Offline
    H Offline
    hix_boz
    wrote on last edited by
    #1

    Hello all,

    I am creating an interface which controlled only by keyboard. I have edit the tab order properly so far and focusing widget is changing when pressing tab key. My problem is I could not use the "Enter" key on focus widget. I thought if I will be able to emit signal when the tab key press, than I can detect the which widget has focused and emitting the different signals based on the focused widget.
    Up to now, I tried the "focusInEvent", "focusOutEvent" and "keyPressEvent" but unfortunately I couldn't make it work. If you suggest the different method, I would like to hear it. Any help or idea would be appreciated.

    J.HilkJ 1 Reply Last reply
    0
    • H hix_boz

      Hello @J-Hilk,

      I used "keyPressEvent" like this and I did not get any reply.

      void MainWindow::keyPressEvent(QKeyEvent *event){
          qDebug() << "Key Pressed" << event->key();
          QKeyEvent* key = static_cast<QKeyEvent*>(event);
      
          if(key->key() == Qt::Key_Tab){
              qDebug() << "Tab is pressed";
          }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #6

      @hix_boz @Christian-Ehrlicher is correct, that event hast to be overwritten in the object that has the focus.

      alternatively, you could use the code as a general eventFilter, installed on your QApplication
      https://doc.qt.io/qt-5/qobject.html#installEventFilter
      or on your tabed widgets


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

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

        You can override QWidget::keyPressEvent() and emit a signal there.

        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
        • H hix_boz

          Hello all,

          I am creating an interface which controlled only by keyboard. I have edit the tab order properly so far and focusing widget is changing when pressing tab key. My problem is I could not use the "Enter" key on focus widget. I thought if I will be able to emit signal when the tab key press, than I can detect the which widget has focused and emitting the different signals based on the focused widget.
          Up to now, I tried the "focusInEvent", "focusOutEvent" and "keyPressEvent" but unfortunately I couldn't make it work. If you suggest the different method, I would like to hear it. Any help or idea would be appreciated.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          hi @hix_boz and welcome

          @hix_boz said in How to emit signal when detecting TAB key press?:

          "focusInEvent", "focusOutEvent" and "keyPressEvent"

          how did you do that? can you share some code and explain more what did not work as expected, because at the very least overriding KeyPressEvent should have worked!


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          H 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            hi @hix_boz and welcome

            @hix_boz said in How to emit signal when detecting TAB key press?:

            "focusInEvent", "focusOutEvent" and "keyPressEvent"

            how did you do that? can you share some code and explain more what did not work as expected, because at the very least overriding KeyPressEvent should have worked!

            H Offline
            H Offline
            hix_boz
            wrote on last edited by
            #4

            Hello @J-Hilk,

            I used "keyPressEvent" like this and I did not get any reply.

            void MainWindow::keyPressEvent(QKeyEvent *event){
                qDebug() << "Key Pressed" << event->key();
                QKeyEvent* key = static_cast<QKeyEvent*>(event);
            
                if(key->key() == Qt::Key_Tab){
                    qDebug() << "Tab is pressed";
                }
            }
            
            J.HilkJ 1 Reply Last reply
            1
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              You have to catch the keyPressEvent() on the widgets where you want to catch the Tab-Key and not on the MainWindow which contains those widgets.

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

              H 1 Reply Last reply
              3
              • H hix_boz

                Hello @J-Hilk,

                I used "keyPressEvent" like this and I did not get any reply.

                void MainWindow::keyPressEvent(QKeyEvent *event){
                    qDebug() << "Key Pressed" << event->key();
                    QKeyEvent* key = static_cast<QKeyEvent*>(event);
                
                    if(key->key() == Qt::Key_Tab){
                        qDebug() << "Tab is pressed";
                    }
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @hix_boz @Christian-Ehrlicher is correct, that event hast to be overwritten in the object that has the focus.

                alternatively, you could use the code as a general eventFilter, installed on your QApplication
                https://doc.qt.io/qt-5/qobject.html#installEventFilter
                or on your tabed widgets


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                H 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  You have to catch the keyPressEvent() on the widgets where you want to catch the Tab-Key and not on the MainWindow which contains those widgets.

                  H Offline
                  H Offline
                  hix_boz
                  wrote on last edited by
                  #7

                  Hi @Christian-Ehrlicher,

                  Thanks for the reply. How do I do what you suggest? I got buttons on centralwidget in mainwindow. I want to know just which button focused. Could you explain that simple code snippet?

                  jsulmJ 1 Reply Last reply
                  0
                  • H hix_boz

                    Hi @Christian-Ehrlicher,

                    Thanks for the reply. How do I do what you suggest? I got buttons on centralwidget in mainwindow. I want to know just which button focused. Could you explain that simple code snippet?

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @hix_boz said in How to emit signal when detecting TAB key press?:

                    How do I do what you suggest?

                    In the same way you did for MainWindow.
                    If yaou need this for QPushButton, then subclass QPushButton, override keyPressEvent there and use that custom button instead of QPushButton.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @hix_boz said in How to emit signal when detecting TAB key press?:

                      How do I do what you suggest?

                      In the same way you did for MainWindow.
                      If yaou need this for QPushButton, then subclass QPushButton, override keyPressEvent there and use that custom button instead of QPushButton.

                      H Offline
                      H Offline
                      hix_boz
                      wrote on last edited by
                      #9

                      Hi @jsulm,

                      Thanks for helping. Could you please share the simple code snippet ? I could not imagine the implementation of it.

                      jsulmJ 1 Reply Last reply
                      0
                      • H hix_boz

                        Hi @jsulm,

                        Thanks for helping. Could you please share the simple code snippet ? I could not imagine the implementation of it.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @hix_boz What code snippet?
                        You did it already for your MainWindow, for QPushButton you do it in exactly same way...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • J.HilkJ J.Hilk

                          @hix_boz @Christian-Ehrlicher is correct, that event hast to be overwritten in the object that has the focus.

                          alternatively, you could use the code as a general eventFilter, installed on your QApplication
                          https://doc.qt.io/qt-5/qobject.html#installEventFilter
                          or on your tabed widgets

                          H Offline
                          H Offline
                          hix_boz
                          wrote on last edited by
                          #11

                          Hi @J-Hilk,

                          Thanks for sharing, it solves the problem.

                          1 Reply Last reply
                          1

                          • Login

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