Search code examples
qtqabstractbutton

Two shortcuts for a QAbstractButton


I need to assign two shortcuts to a QAbstractButton, but I cannot find a way to do that.

It seems the only method is QAbstractButton::setShortcut(const QKeySequence & key).

Is it possible ?


Solution

  • QAbstractButton only accept one shortcut sequence. Try to give to shortcut in QtDesigner like Alt+A & Alt+Z and you will see that you have to do Alt+A AND Alt+Z to execute shortcut. So you can not achieve to have 2 shortcuts as you want.

    But there is a solution:

    All QObject have a function named event that receives all events. You can create your own class that inherits from your class button (QPushButton, etc. or directly from QAbstractButton if you want to have a personalized button) with a list of QShortcutSequence (or a pair if you only want 2 shortcuts) and re-implement the event function to track the QShortcutEvent. Don't forget to release all other events to be process by parents classes.