[Development] QKeySequenceEdit questions

Volker Hilsheimer volker.hilsheimer at qt.io
Mon May 16 21:43:31 CEST 2022



> On 16 May 2022, at 19:44, Laszlo Papp <lpapp at kde.org> wrote:
> 
> 
> 
> I’ll have to look a bit more, but looking at the code I see that QKeySequenceEdit accepts both the ShortcutOverride and the Shortcut event, so it should take precedence over any application-defined shortcut. If/since that doesn’t work, 
> 
> But since you write cmd+q (and considering your recent patches) I think that perhaps you are on macOS, and it might be that our macOS shortcut code doesn't respect the focus widget’s override for application level shortcuts.
> 
> Bottom line anyway: this should already work, so if it doesn’t, I’d consider that a bug.
> 
> Yes, it seems to work on Windows and Mac. I have not noticed until you mentioned it. Thanks for pointing that out.
> 
> 1. Is there an easy workaround until this gets fixed in Qt?
> 2. Where would I need to look if I wanted to help you out by sending a patch for Qt so that future versions will work on Mac?
> 
> Just to be clear on this, even a simple main.cpp reproduces this issue:
> 
> #include <QApplication>
> #include <QKeySequenceEdit>
> 
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
>     QKeySequenceEdit keySequenceEdit;
>     keySequenceEdit.show();
>     return app.exec();
> }

That example is a bit contrived because you don’t create a menu bar with your own actions, so you get the default actions from macOS that Qt has (practically) no control over. So for anything to be possible, you need to create a QMenuBar with the respective actions added.

And this works for most actions in most menus, i.e. in the following example, Cmd+T and Ctrl+D do what they should, i.e. record the key with the editor, and not trigger the shortcut:

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMainWindow window;
    QMenu *fileMenu = window.menuBar()->addMenu("&File");
    fileMenu->addAction("&Quit", []{
        qDebug() << "Quit shortcut triggered";
    });
    fileMenu->addAction("T", QKeySequence(Qt::ControlModifier|Qt::Key_T), []{
        qDebug() << "Ctrl+T";
    });
    QMenu *editMenu = window.menuBar()->addMenu("&Edit");
    editMenu->addAction("E", QKeySequence(Qt::MetaModifier|Qt::Key_E), []{
        qDebug() << "Meta+E";
    });
    QKeySequenceEdit keySequenceEdit;
    window.setCentralWidget(&keySequenceEdit);
    QShortcut sc = QShortcut(QKeySequence(Qt::AltModifier | Qt::Key_D), &keySequenceEdit, []{
        qDebug() << "Alt+D";
    });
    window.show();
    return app.exec();
}

The Cmd+Q however will trigger the shortcut and execute the lambda (which does not quit the application).

From what I see, the problem here is limited to the shortcuts for menu entries that live in the macOS application menu. That menu gets created as a plain NSMenu in qcocoamenuloader.mm, so our delegate that is responsible for respecting the ShortcutOverride handling of the focus widget doesn’t get called. I think that’s easily fixed:

https://codereview.qt-project.org/c/qt/qtbase/+/411608


Volker



More information about the Development mailing list