Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved Filtering shortcut in eventFilter not working

    General and Desktop
    eventfilter shortcut
    4
    12
    2694
    Loading More Posts
    • 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
      mpergand last edited by mpergand

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • M
          mpergand @SGaist last edited by mpergand

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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

            JonB M 2 Replies Last reply Reply Quote 0
            • JonB
              JonB @SGaist last edited by

              @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 Reply Quote 0
              • M
                mpergand @SGaist last edited by mpergand

                @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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • M
                    mpergand last edited by

                    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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 0
                      • M
                        mpergand @SGaist last edited by mpergand

                        @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 Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          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 Reply Quote 0
                          • Q
                            Qiang0203 last edited by

                            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 Reply Quote 0
                            • First post
                              Last post