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. Filtering shortcut in eventFilter not working
QtWS25 Last Chance

Filtering shortcut in eventFilter not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
eventfiltershortcut
12 Posts 4 Posters 3.8k 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.
  • M Offline
    M Offline
    mpergand
    wrote on 24 Jan 2018, 19:29 last edited by mpergand
    #1

    Hi all,
    I'm trying to block/filtering shortcuts regarding some condition in a event filter method at application level, but it has no effect.

    (pseudo) code showing what i'm doing:

    bool Application::eventFilter(QObject *obj, QEvent *event)
    {
    if(event->type() == QEvent::ShortcutOverride)
            {
             QAction* action=actionWithShortcut(obj,event); // get the action 
    
              if(action)
                {
                 action->setEnabled(false); // disable it
                 return true;  // has no effect
                }
    ...
    

    anyway, the shortcut is triggered :(

    My goal is to intercept shortcuts before there're triggered, I thought an event filter could be a good way but to no avail.

    Any idea ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Jan 2018, 20:42 last edited by
      #2

      Hi,

      Shouldn't that be QEvent::Shortcut ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 24 Jan 2018, 21:38
      0
      • S SGaist
        24 Jan 2018, 20:42

        Hi,

        Shouldn't that be QEvent::Shortcut ?

        M Offline
        M Offline
        mpergand
        wrote on 24 Jan 2018, 21:38 last edited by mpergand
        #3

        Hi @SGaist

        I tried allready, such event never occured ...

        For a single key stroke, I see two ShortcutOverride event:

        shortcut override "Couper" "Ctrl+X" TextEdit(0x7f8fae45a5d0)
        shortcut override "Couper" "Ctrl+X" TextDocWindow(0x7f8fae459c80)
        

        Maybe I need to filter in the widgets too ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Jan 2018, 21:44 last edited by
          #4

          Something's not clear, why do you want to disable the action that is associated with the shortcut when you actually call the shortcut ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J M 2 Replies Last reply 24 Jan 2018, 21:55
          0
          • S SGaist
            24 Jan 2018, 21:44

            Something's not clear, why do you want to disable the action that is associated with the shortcut when you actually call the shortcut ?

            J Offline
            J Offline
            JonB
            wrote on 24 Jan 2018, 21:55 last edited by
            #5

            @SGaist
            I wondered that earlier too :) I think you can ignore the action->setEnabled(false); // disable it, I read the OP's question as why does return true; // has no effect have no effect?

            1 Reply Last reply
            0
            • S SGaist
              24 Jan 2018, 21:44

              Something's not clear, why do you want to disable the action that is associated with the shortcut when you actually call the shortcut ?

              M Offline
              M Offline
              mpergand
              wrote on 24 Jan 2018, 21:56 last edited by mpergand
              #6

              @SGaist said in Filtering shortcut in eventFilter not working:

              why do you want to disable the action that is associated with the shortcut when you actually call the shortcut ?

              Yes and I want that the action was not triggered (logical ?)
              Why ? yes I know, it's not the common way to do.
              Actually, I try to implement a responder chain for the menu management.

              What I see, is that the action method is called so I need an extra test in it:

              void Application::menuAction(QAction* action)
              {
                  // validateAction dans event filter sans effet
              // test action state
                  if(!action->isEnabled()) return;
              ...
              

              In addition on Mac, the menu is flashing but no action takes place in fact.
              But if I do the same key stroke a second time, the action is not triggered, so the action state (disabled) is not take in account immediately in the event filter.
              That's why I would like to discard the shortcut event.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 24 Jan 2018, 22:52 last edited by
                #7

                Again, that sounds counter-intuitive and convoluted. Why not just disable the action when it should not be used ?

                What chain do you want to build with that ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mpergand
                  wrote on 24 Jan 2018, 23:34 last edited by
                  #8

                  It turns out, that the filter works well, if I return yes in the event filter, the shortcut is not propagated to the TextEdit, so far so good.

                  The final question is, who triggers the action ?

                  @SGaist
                  The "Responder Chain" is a very good paradigm for menu management, unfortunatly not easy to implement in Qt.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Jan 2018, 22:35 last edited by
                    #9

                    Unless we have a different definition of Responder Chain, it's what Qt implements. Hence my question about what you are trying to achieve.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply 26 Jan 2018, 05:31
                    0
                    • S SGaist
                      25 Jan 2018, 22:35

                      Unless we have a different definition of Responder Chain, it's what Qt implements. Hence my question about what you are trying to achieve.

                      M Offline
                      M Offline
                      mpergand
                      wrote on 26 Jan 2018, 05:31 last edited by mpergand
                      #10

                      @SGaist said in Filtering shortcut in eventFilter not working:

                      Unless we have a different definition of Responder Chain, it's what Qt implements.

                      I agree, but does it apply with menu actions ?
                      What I want to do, is the same thing as validateMenuItem on MacOS.
                      validatemenuitem

                      On Mac, this method is called before a menu appears (menuAboutToShow on Qt), but also before a shortcut is triggered, allowing to validate the menu at the time the action occurs.
                      That what I'm trying to do with a eventFilter.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 26 Jan 2018, 22:23 last edited by
                        #11

                        With Qt, you usually trigger a signal when conditions are met that you connect to the setEnable of the QAction you want. Or you modify the action object directly.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qiang0203
                          wrote on 26 Oct 2022, 15:34 last edited by
                          #12

                          I think you need to explicitly accept the event to trigger the override, as mentioned in the QKeyEvent doc.
                          Call "event->accept()" before "return true" may resolve your problem.

                          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