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 handle keypress event for all child widgets, in Qt?

How to handle keypress event for all child widgets, in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
keypresseventeventfilter
13 Posts 2 Posters 16.5k 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.
  • N NIXIN
    25 Aug 2016, 08:24

    sorry for the typing error, I have changed it.

    Now please help

    R Offline
    R Offline
    raven-worx
    Moderators
    wrote on 25 Aug 2016, 08:26 last edited by raven-worx
    #4

    @NIXIN
    so somesignal1() and somesignal2() will only be emitted when the corresponding buttons also have the focus while you press the F1-key.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NIXIN
      wrote on 25 Aug 2016, 08:28 last edited by
      #5

      what kind of focus

      can please provide a small code as an example

      R 1 Reply Last reply 25 Aug 2016, 08:51
      0
      • N NIXIN
        25 Aug 2016, 08:28

        what kind of focus

        can please provide a small code as an example

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 25 Aug 2016, 08:51 last edited by raven-worx
        #6

        @NIXIN
        there is only one kind of (input-)focus.
        Only the focused widget receives input events (like key and mouse events). When they do not process them, the event is delivered to it's parent widget, and so on and on.

        Either you set the focus manually using QWidget::setFocus(), or by mouse or tab key navigation, etc.

        I think in your case its anyway better to use QShortcut instead of this eventFilter approach:

        QShortcut* shortcut = new QShortcut(QKeySequence("F1"), this, SLOT(onF1ShortcutTriggered()), SLOT(onF1ShortcutTriggered()), Qt::WindowShortcut)
        

        And just add a onF1ShortcutTriggered() slot to your class.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • N Offline
          N Offline
          NIXIN
          wrote on 25 Aug 2016, 09:23 last edited by
          #7

          If I am using eventFilter(), then where should I implement this QWidget::setFocus()

          1 Reply Last reply
          0
          • R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 25 Aug 2016, 10:30 last edited by
            #8

            it seems to me that you don't quite understand what the "focus" means yet.
            It's rather uncommon to set the focus just so you might (or might not) receive a shortcut key-press on a widget.

            Maybe you should explain more what you want to achieve exactly.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • N Offline
              N Offline
              NIXIN
              wrote on 25 Aug 2016, 10:57 last edited by
              #9

              suppose there are several children widgets(here button1, button2) inside parent widget(here gBox). I want to provide help feature for these widget, such that if F1 is pressed for a particular widget it will emit a signal and html page will be opened in the corresponding slot.

              R 1 Reply Last reply 25 Aug 2016, 11:18
              0
              • N NIXIN
                25 Aug 2016, 10:57

                suppose there are several children widgets(here button1, button2) inside parent widget(here gBox). I want to provide help feature for these widget, such that if F1 is pressed for a particular widget it will emit a signal and html page will be opened in the corresponding slot.

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 25 Aug 2016, 11:18 last edited by raven-worx
                #10

                @NIXIN
                well and why dont you accept my suggested approach using QShortcut?!
                You can even set a different QShortcut for every widget, with a different signal:

                new QShortcut(QKeySequence("F1"), button1, SIGNAL(somesignal1()), SIGNAL(somesignal1()), Qt::WidgetShortcut);
                new QShortcut(QKeySequence("F1"), button2, SIGNAL(somesignal2()), SIGNAL(somesignal2()), Qt::WidgetShortcut);
                ...
                

                Basically there is nothing wrong with the event filter approach and this approach works the same, since (like every other approach) needs the focus on the widget. And it is a one-liner. Which makes also perfect sense.

                Maybe the "Whats this" feature is of interest for you. This is rather focus independent, but also works differently.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NIXIN
                  wrote on 25 Aug 2016, 12:09 last edited by
                  #11

                  I tried your suggested approach of using QShortcut

                  Pressing F1, is working for parent widgets also.

                  I want spacifically for child widget only

                  R 1 Reply Last reply 25 Aug 2016, 12:36
                  0
                  • N NIXIN
                    25 Aug 2016, 12:09

                    I tried your suggested approach of using QShortcut

                    Pressing F1, is working for parent widgets also.

                    I want spacifically for child widget only

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 25 Aug 2016, 12:36 last edited by raven-worx
                    #12

                    @NIXIN
                    as i said...this only works if the button has input focus.
                    This is mostly the case when the user clicked on it.

                    An alternative is that you show the help according to the current mouse position at the time F1 is pressed. For that you can do this:

                    qApp->installEventFilter( this );
                    ...
                    bool eventFilter( QObejct* watched, QEvent* event )
                    {
                        switch( event->type() )
                        {
                              case QEvent::KeyPress:
                              {
                                      QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                                      if(keyEvent->key() == Qt::Key_F1)
                                      {
                                             if( QWidget* w = QApplication::widgetAt( QCursor::pos() ) )
                                             {
                                                     if( w == XXX )
                                                     {
                                                            // show help
                                                            return;
                                                      }
                                             }
                                      }
                              }
                              break;
                         }
                    
                         return BaseClass::eventFilter( watched, event );
                    }
                    

                    don't know if thats enough for you.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    1
                    • N Offline
                      N Offline
                      NIXIN
                      wrote on 26 Aug 2016, 13:36 last edited by
                      #13

                      That worked fine, thanx a lot for your help

                      1 Reply Last reply
                      0

                      13/13

                      26 Aug 2016, 13:36

                      • Login

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