Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Ambiguous shortcut overload



  • Hello everyone,

    I ran into a problem with shortcuts triggering, in a quite large application, on the Mac.

    The situation :
    I have an application-wide QAction to copy my main document's selection, tied to the QKeySequence::Copy shortcut, with (a priori) Qt::ApplicationShortcut context.

    Then, in a large modal QDialog (almost another application by itself) :

    • I define another QAction, tied to the QKeySequence::Copy shortcut, with (a priori) Qt::WidgetShortcut context.
    • i add this QAction to a non-native menu in a menubar (yes, I know that having a menubar in a dialog is not best practice ; but I have reasons for that !)

    When the Dialog is opened, the dialog's copy action is never triggered.
    Instead, I get the (famous ?) debug message :
    QAction::eventFilter: Ambiguous shortcut overload: ?C

    I tried all the possible combinations of the 4 possible values for the 2 shortcut contexts. No effect.

    My "hack" :
    The only solution I got was to process manually the shortcut :
    1/ override the QDialog::event() method, with :

    bool MyDialog::event(QEvent *event){
      if(event->type() == QEvent::ShortcutOverride){
        QKeyEvent* kpe = dynamic_cast<QKeyEvent*>(event);
         if (kpe->matches(QKeySequence::Copy)) {
           kpe->accept();
           return true;
         }
       }
      return QDialog::event(event);
    }
    

    AND 2/ override the QDialog::keyPressEvent() method with :

    void MyDialog::keyPressEvent ( QKeyEvent * event ) {
      if (event->matches(QKeySequence::Copy)) {
        m_copyAction->trigger();
        event->accept();
       return;
      } 
      QDialog::keyPressEvent(event);
    }
    

    With both these two hand-made methods, the action is triggered in the context of my dialog.
    (BTW, I dont understand why I need overriding both these methods...)

    My question are :
    1 - do you know a good complete documentation explaining in detail how shortcut resolution is achieved, especially on the Mac ?
    I have had indeed many such shortcut problem in the past 15 years with Qt4, and so far, I did not find a comprehensive doc.

    2- would you say that my hand-made hack is appropriate ?
    If yes, is there a reason why I have to go back to the event-handling methods to solve a (rather simple) shortcut issue ?

    3- I could not reproduce the pb with a simple example. Probably, there may be some bad code somewhere in my (rather large) app. But I cannot figure out where. Any idea where I should look ?

    I thank you very much for your attention,
    Nicolas


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    1. That will require reading Qt's sources
    2. It should not be needed
    3. The first things that comes to my mind would to check what you do with all your actions. For example, are they set on several widgets ? Are they re-used ?

    Also, since it's a copy action, do you really need it ? For example, text inputs already handle that.


Log in to reply