Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-107526

action Cmd+Return does not emit shortcut override event on macOS

    XMLWordPrintable

Details

    • macOS
    • 0835537c3 (dev), 091160a9a (6.5), 598022cdc (6.4)

    Description

      If an action with Cmd+Return shortcut is present in the main application menu, then an event with type QEvent::ShortcutOverride is not invoked when this shortcut is pressed. The problem seems to be on macOS only, the corresponding Ctrl+Return works well on Windows and Linux. The problem curiously does not exist if the action is not present in the main menu or is disabled. This problem also does not exist for many other 'innocent' shortcuts such as Cmd+R, Cmd+I, etc.

      The consequence of this problem is that you cannot override the application-wide Cmd+Return shortcut and handle this shortcut locally in a widget by handling its corresponding key press event.

      The following code is to illustrate the problem. There are two global shortcuts but each of them behaves different. I try to block these global shortcuts by setting shortcut override event as accepted in the event filter and then processing the key press event locally. But blocking of Cmd+Return does not work because QEvent::ShortcutOverride event is not issue at all.

      #include <QAction>
      #include <QApplication>
      #include <QDebug>
      #include <QKeyEvent>
      #include <QMainWindow>
      #include <QMenu>
      #include <QMenuBar>
      #include <QTextEdit>
      
      class MainWindow : public QMainWindow
      {
      public:
          MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
          {
              auto menu = new QMenu("Menu");
              auto menuBar = new QMenuBar();
              menuBar->addMenu(menu);
              setMenuBar(menuBar);
      
              auto action1 = new QAction("Action 1");
              action1->setShortcut({Qt::CTRL | Qt::Key_Return}); // evil "unblockable" shortcut
              menu->addAction(action1);
              connect(action1, &QAction::triggered, this, []{ qDebug() << "action 1"; } );
      
              auto action2 = new QAction("Action 2");
              action2->setShortcut({Qt::CTRL | Qt::Key_R}); // innocent "blockable" shortcut
              menu->addAction(action2);
              connect(action2, &QAction::triggered, this, []{ qDebug() << "action 2"; } );
      
              auto textEdit = new QTextEdit();
              setCentralWidget(textEdit);
              textEdit->installEventFilter(this);
          }
      
      protected:
          bool eventFilter(QObject *watched, QEvent *event) override
          {
              if (event->type() == QEvent::ShortcutOverride)
              {
                  // setting as accepted blocks the global shortcut
                  auto keyEvent = static_cast<QKeyEvent*>(event);
                  qDebug() << "shortcut override" << keyEvent;
                  event->setAccepted(true); 
                  return true;
              }
      
              if (event->type() == QEvent::KeyPress)
              {
                  // this is the local key shortcut handler
                  auto keyEvent = static_cast<QKeyEvent*>(event);
                  qDebug() << "key press" << keyEvent;
                  return true;
              }
      
              return false;
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      PS: I found the same problem is with Cmd+Shift+Return. So there may be even more such problematic shortcuts which cannot be overridden.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              vhilshei Volker Hilsheimer
              vladimir.kraus Vladimir Kraus
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes