Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Shortcuts doesn't work on Qt app on OSX
Forum Updated to NodeBB v4.3 + New Features

Shortcuts doesn't work on Qt app on OSX

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    KelvinSP
    wrote on last edited by KelvinSP
    #1

    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
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      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
      0
      • K Offline
        K Offline
        KelvinSP
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          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
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved