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. QAction shortcuts not working in Qt5 and Mac OS
Forum Updated to NodeBB v4.3 + New Features

QAction shortcuts not working in Qt5 and Mac OS

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 3.7k Views
  • 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.
  • S Offline
    S Offline
    SteveB
    wrote on last edited by
    #1

    I have a Qt5 application that was developed on Debian Linux. I am porting it to Mac OS and enhancing it. When I ported it, all of the menu action shortcuts came over and had been changed in Qt Creator to reflect Mac's command key rather than the control key. The XML in the ui file still looks the same.

    <action name="LoginAction">
    <property name="text">
    <string>Log in...</string>
    </property>
    <property name="shortcut">
    <string>Ctrl+L</string>
    </property>
    </action>

    However, the only shortcut that works is command-Q. I have tried adding the shortcuts programmatically:

    #if defined (Q_OS_MACX)
    ui->LoginAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
    #endif

    but they still don't work.

    Any help would be appreciated.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      try:
      Qt::META+Qt::Key_L

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SteveB
        wrote on last edited by
        #3

        No joy. It did change the modifier symbol from the command thingie to a carat, but the shortcut still doesn't work. Even tried control-L.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          My bad, Qt::META is the CTRL key on Mac.

          I'm using this in one of my app:

          action->setShortcut(QKeySequence("Ctrl+Shift+B"));
          action->setShortcutContext(Qt::ApplicationShortcut);
          

          On Mac it becomes ⇧⌘B

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SteveB
            wrote on last edited by
            #5

            Still no luck.

            In the code I posted, I had just used the enumerators rather than a string, but neither method works. Adding the invocation of setShortcutContext didn't solve the problem.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              What is your version of Qt ?

              If you click on the menu, is the action triggered ?

              You can try with a predefined key sequence to see if it works:

              QAction* action=menu->addAction(tr("New"));
              action->setShortcut(QKeySequence::New);
              

              You need to create a default menu with no parent, in the case no more window is opened.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SteveB
                wrote on last edited by
                #7

                Still not working. This leads me to believe that the problem is with Cocoa grabbing the key sequence.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SteveB
                  wrote on last edited by
                  #8

                  The issue is with using a menu that is not native to OS X. The shortcuts from the non-native menu actions don't seem to trigger anything. In the UI designer view, you need to to check the native menu box in the top level menu properties, and you need to make the shortcuts application level shortcuts. There is a checkbox on the QAction properties for this.

                  You can also do it programmatically like so:

                  QAction *myAction = new QAction("My action");
                  myAction->setShortcut(QKeySequence("CTRL+M"));
                  myAction->setShortcutContext(Qt::ApplicationShortcut);
                  
                  QMenu *myMenu = new QMenu("My Menu");
                  myMenu->addAction(myAction);
                  ui->menuBar->addMenu(myMenu);
                  
                  ui->menuBar->setNativeMenuBar(true);
                  
                  1 Reply Last reply
                  0

                  • Login

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