Please nominate your Qt Champions for 2021!

How to Use QAction for keyboard shortcuts

  • Hello, I am a beginner QT/Visual studios user (c++). I am trying to make a keyboard shortcut for one of my methods in my code. The command is for an emergency stop, i want to implement a USB button that can be configured with a keyboard shortcut. so when the button is pressed, it "types" a string of characters which can be used as a shortcut.

    so what i am trying to do is to use the shortcut assigned to the button in my visual studios code, so essentially when the button is pressed the actions for emergencystop within the code are initiated. I just dont know how to use QAction - the example on the QT website has just confused me further.

  • @SUMI

    You could either use QAction shortcuts or use an event filter, which reacts on the simulated button press events.

    Maybe there are even more ways. How is your USB button connected to your program?
    (via USB obviously, but how is your OS reacting on this button press? Does it already send a pre-defined key command or was that just your idea?)


    Just setting a keyboard shortcut to a QAction is not that difficult.

    QAction *action = new QAction(this);
    action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_J)); // make sure your key sequence is not used in this context

  • @Pl45m4 Thank you for replying :). The button is from it isnt already pre-defined you do that yourself, that was the easy bit, so when i press the button its literally like i typed "shift+F1". so i need to tell qt that when "shift+f1" is pressed i want it to initialise the emergencystop method. It seems pretty simple but i keep getting myself confused.

    so this is what ive managed to do:

    class KeyboardShortcutData
    KeyboardShortcutData(QList<QKeySequence> keySequences, void (emergencyStopPressed)()) :
    keySequences(keySequences), slot(emergencyStopPressed)

    	QList<QKeySequence> keySequences;
    	void (EngineeringGUI::emergencyStopPressed();

    and then

    /Keyboard Estop
    void EngineeringGUI::addShortcuts(QWidget* shortcutWidget, const QList<QKeySequence>& keySequences, void(EngineeringGUI::* slot)())
    QAction* action = new QAction(this);
    connect(action, &QAction::triggered, this, slot);

    const QMap<QString, KeyboardShortcutData> KEYBOARD_SHORTCUTS
    { "Emergency Stop Pressed", KeyboardShortcutData({ { ("Shift+F1") } }, &EngineeringGUI::emergencyStopPressed) }

    all this just came from me trying to piece together - it doesnt build as you can imagine

  • @SUMI said in How to Use QAction for keyboard shortcuts:


    If EngineeringGUI is your class, you want to use your shortcut in, you did waaay too much unnecessary stuff :)
    You dont need to create a new class for this shortcut or your connection.

    [ . . . ] create QActions with the shortcut key sequences you require (if you also want equivalent menu options and toolbar buttons), or create QShortcuts if you just need key sequences. Both QAction and QShortcut handle all the event filtering for you, and provide signals which are triggered when the user triggers the key sequence, so are much easier to use than this low-level function.

    So you dont even need QActions

    Try this:

    QShortcut *sC = new QShortcut(QKeySequence(Qt::Key_Shift + Qt::Key_F1), this);
    connect(sC, &QShortcut::activated, this, &EngineerGUI::emergencyStopPressed);

  • @Pl45m4 thank you! so helpful :)
    I tried it and it build fine with no errors, but when try it on the gui it doesnt work. not sure why but when i do Shift+f1 nothing happens - it might be something else that is the problem, i am going to try debug

Log in to reply