python-3.xkeyboard-shortcutsqt-designerpyqt5qpushbutton

QT Designer how to assign 2 hotkeys to a QPushButton


I created a simple GUI and assigned "Enter" to QPushButton ("Begin"). This is the line for it:

self.Begin.setShortcut(_translate("Form", "Enter"))

Evrything works perfectly, BUT how to assign 2 variants of hotkey for the same button? I want the button to react to 2 hotkeys: Enter and Return (Usual "Big Enter" and "Small Enter" on the NumPad)

Thanks in advance.


Solution

  • There are several ways to do this. Probably the simplest is to use QShortcut:

    QShortcut(Qt.Key_Enter, self.Begin, self.handleBegin)
    QShortcut(Qt.Key_Return, self.Begin, self.handleBegin)
    

    To get the button animation behaviour, try this instead:

    QShortcut(Qt.Key_Enter, self.Begin, self.Begin.animateClick)
    QShortcut(Qt.Key_Return, self.Begin, self.Begin.animateClick)