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. How to Use QAction for keyboard shortcuts

How to Use QAction for keyboard shortcuts

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 4.8k 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
    SUMI
    wrote on last edited by
    #1

    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.

    Pl45m4P 1 Reply Last reply
    1
    • S SUMI

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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?)

      Edit:

      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
      

      https://doc.qt.io/qt-5/qaction.html#shortcut-prop


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply
      2
      • Pl45m4P Pl45m4

        @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?)

        Edit:

        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
        

        https://doc.qt.io/qt-5/qaction.html#shortcut-prop

        S Offline
        S Offline
        SUMI
        wrote on last edited by
        #3

        @Pl45m4 Thank you for replying :). The button is from http://www.usbbutton.com/ 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
        {
        public:
        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);
        action->setShortcuts(keySequences);
        connect(action, &QAction::triggered, this, slot);
        shortcutWidget->addAction(action);
        }

        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

        Pl45m4P 1 Reply Last reply
        0
        • S SUMI

          @Pl45m4 Thank you for replying :). The button is from http://www.usbbutton.com/ 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
          {
          public:
          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);
          action->setShortcuts(keySequences);
          connect(action, &QAction::triggered, this, slot);
          shortcutWidget->addAction(action);
          }

          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

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

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

          EngineeringGUI

          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.
          (from: https://doc.qt.io/qt-5/qwidget.html#grabShortcut)

          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);
          

          https://doc.qt.io/qt-5/qshortcut.html#activated


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          S 1 Reply Last reply
          2
          • Pl45m4P Pl45m4

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

            EngineeringGUI

            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.
            (from: https://doc.qt.io/qt-5/qwidget.html#grabShortcut)

            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);
            

            https://doc.qt.io/qt-5/qshortcut.html#activated

            S Offline
            S Offline
            SUMI
            wrote on last edited by
            #5

            @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

            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