QKeySequence - multiple keys

  • I am trying to create shortcuts for menu actions, which include multiple keys. For the actions directly in the menu list, I am using shortcuts like "Ctrl + 1", etc. which works just fine.

    @QAction *qa_enableLighting = viewer_menu->addAction("Enable Lighting", this, SLOT(on_action_enableLighting_triggered()), QKeySequence(Qt::CTRL + Qt::Key_1));@

    However, for an action under a sub-menu, I would like to use multiple keys, for example, "Ctrl + V + 1". When I try to create this keysequence, it does not work, i.e. the shortcut just doesn't show up.

    @QAction *qa_submenu = submenu->addAction("Test 1", this, SLOT(on_action_submenu1_triggered()), QKeySequence(Qt::CTRL + Qt::Key_V + Qt::Key_1));@

    Instead, when I used
    @QKeySequence(Qt::CTRL + Qt::Key_M + Qt::Key_1))@

    the shortcut showed up as "Ctrl+~". I understand that Ctrl+V is a standard shortcut, and probably that is why Qt is not allowing me to use it, while "M+1" is creating "~". However, is there a way to enable using sequences of the following form ?

    @Ctrl + <key_of_my_choice> + 1@

  • Lifetime Qt Champion


    You can't do it like that. QKeySequence's documentation explains why: a shortcut is composed of one Qt::Key and 0 or more modifiers.

  • Thanks for the pointer, SGaist. But is there an easy way to use multiple keys ?

  • Lifetime Qt Champion

    I would say an event filter where you keep track of the keys pressed and act accordingly

Log in to reply

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