Shortcuts doesn't work on Qt app on OSX



  • I have developed a Qt application on Windows and added some actions to the menu bar in the main window. I have also added some shortcuts for the actions like Ctrl+O, Ctrl+S and Ctrl+Q, as can be seen in the image below.

    enter image description here


    Now, when I open and run the application on Qt on OSX, the shortcuts were automatically mapped to use Command instead of Control, as you can see in the following image:

    enter image description here

    The problem is that the shortcuts are not working properly on OSX. The Command+Q works fine and closes the application but the Command+S and Command+O don't work.

    Any idea what is going on? There is a way or a workaround to solve it?


    Step-by-Step

    I have created the actions using the Qt Design, so here is a step-by-step to reproduce it:

    Create a new project and in the main window add a menu (test1) and an action (test2):

    enter image description here

    Double-click on the actionTest2 in the list will open the Edit Action window where it was added the shortcut Ctrl+S:

    enter image description here

    Now the actionTest2 is triggered by the Ctrl+S shortcut:

    enter image description here

    In the triggered slot it was just included a QDebug to find out if it is being called:

    void MainWindow::on_actionTest2_triggered()
    {
        qDebug() << "Action Test2 Triggered";
    }
    

    It works well on Windows and when I press Ctrl+S it shows the Action Test2 Triggered message. Now if I compile and run the application on OSX and tries to press Ctrl+S or Command+S nothing happens.

    Note: It can also be created by the following code:

    QAction *actionOpen = new QAction("Open Action", this);
    actionOpen->setShortcut(QKeySequence(Qt::Key_O | Qt::CTRL));
    addAction(actionOpen);
    
    connect(actionOpen, SIGNAL(triggered()), this, SLOT(on_actionTest2_triggered()));
    
    QMenu *menu = menuBar()->addMenu(QString(tr("Menu")));
    menu->addAction(actionOpen);
    

    I'm using Qt 5.3.


    I have found three related questions here, here and here, but I could not find a solution yet.

    Based on the answers of these questions I have tried to add the following code to the main window constructor, but it didn't work:

    #ifdef Q_OS_MAC
        ui->actionOpen->setShortcut(QKeySequence(Qt::Key_O | Qt::CTRL));
        ui->actionOpen->setShortcutContext(Qt::ApplicationShortcut);
    #endif
    

  • Lifetime Qt Champion

    Hi,

    I did a quick test with:

    MainWindow::MainWindow(QWidget *parent)
         : QMainWindow(parent)
    {
        QAction *action = new QAction(this);
        action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
    
        connect(action, &QAction::triggered, this, &MainWindow::testMe);
    
        addAction(action);
    }
    
    void MainWindow::testMe()
    {
        qDebug() << Q_FUNC_INFO;
    }
    

    and it worked properly.



  • @SGaist, you're right. The example I have shown does not replicate the issue and works fine on OSX.

    I have found that the issue was happening because I was setting the setNativeMenuBar to false.

    In my MainWindow constructor I had this line:

    ui->menuBar->setNativeMenuBar(false);
    

    I was not using the native menu bar because I had a menu called Settings that was not working properly on OSX. Now, I have changed the menu name from Settings to Tools and removed the ui->menuBar->setNativeMenuBar(false); command and everything is working fine.

    So, basically, some shortcuts (e.g. Command+S and Command+O) doesn't work when setting the setNativeMenuBar to false. Probably the OSX uses these shortcuts for other purposes, so the application could not use it.

    Anyway, thank you very much for the help.


  • Lifetime Qt Champion

    Sounds strange... But glad you could get it to work :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.