Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

action to be triggered based on its custom shortcut



  • suppose i have some action to happen. for that i can create an QAction object and connect its triggered signal to the slot that executes the desired function. also, i can have a shortcut associated with the action; changing the shortcut i'll be able to execute the same action with that shortcut.

    my problem now is that the "shortcut" i wanna set to the action, contains also a mouse button press (and mouse events cannot be assigned to action shortcuts); say i want Shift+Left mouse button. maybe this sounds a little bit harsh but bear with me.

    what do i need? well, i have a button, and an action (say "execute a script"). i want the script to execute when Shift+Left click is clicked, and i want this "shortcut" to be customized, i.e. the user should be able to change to shortcut to, say Ctrl+Left click (from some gui element, e.g. button text), and now Ctrl+Left click should execute the script.

    how can i do this?


  • Lifetime Qt Champion

    Hi
    Well the shortcut system cannot be tied to mouse events so you have to make a special launch button
    that has a overwritten mousePress function.
    In that function you can use
    http://doc.qt.io/qt-5/qguiapplication.html#queryKeyboardModifiers
    to see if ctrl, shift , alt is pressed and then emit a signal to have the
    script holder execute the script.

    For editing it, you can use a line edit and let user somehow select the keys and mouse button.
    (maybe from comboboxes )

    [edit: fixed typo SGaist]


Log in to reply