4

In PyQt, how would you tie in two shortcuts for one button? I have scoured the QT documentation and StackOverflow, but the only thing I found was Two shortcuts for one action, but that was for QT and I could not wrap my head around that.

1 Answers1

7

The only difference is that instead of using QList you should use the list:

from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        menubar = self.menuBar()
        menu = menubar.addMenu("File")
        foo_action = menu.addAction("foo")
        foo_action.setShortcuts(["Ctrl+1", "Ctrl+S"])
        foo_action.triggered.connect(self.on_triggered)

    @QtCore.pyqtSlot()
    def on_triggered(self):
        print("on_triggered")

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

In the case of QWidgets they do not have the setShortcuts() method, so one option is to use QShortcuts:

for sequence in ("Enter", "Return",):
    shorcut = QtWidgets.QShortcut(sequence, self.pushButton_eq)
    shorcut.activated.connect(self.pushButton_eq.animateClick)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I did this, `self.pushButton_eq.setShortcut(_translate("MainWindow", ["Return", "Enter"]))`, but I got `TypeError: translate(str, str, disambiguation: str = None, n: int = -1): argument 2 has unexpected type 'list'` – DS_Secret Studios Dec 27 '18 at 19:36
  • My programs full code is available [here](https://github.com/DSSecret-Studios-Dev/Virtual-Desktop). It's at `./Calculator/MainWindow.py`. – DS_Secret Studios Dec 27 '18 at 19:38
  • @DS_SecretStudios Why do you use `_translate`? – eyllanesc Dec 27 '18 at 19:38
  • @DS_SecretStudios On the other hand in your question you point to an action (QAction) not a QPushButton. – eyllanesc Dec 27 '18 at 19:42
  • That one button runs a function in my program, thus I though I could hook it up to the button. – DS_Secret Studios Dec 27 '18 at 19:44
  • @DS_SecretStudios change `self.pushButton_eq.setShortcut(_translate("MainWindow", "Return")) self.pushButton_eq.setShortcut(_translate("MainWindow", "Enter"))` to `for sequence in ("Enter", "Return",): QtWidgets.QShortcut(sequence, self.pushButton_eq, activated=self.pushButton_eq.animateClick)` – eyllanesc Dec 27 '18 at 20:02
  • @DS_SecretStudios I have updated my answer indicating the solution for your case, also do not use translate. On the other hand if my answer helps you do not forget to mark it as correct, if you do not know how to do it then review the [tour], that is the best way to thank. – eyllanesc Dec 27 '18 at 20:07
  • `foo_action.setShortcuts(["Ctrl+1", "Ctrl+S"])` works but only shows the first shortcut. When I swap them always the first is shown!!?? Why is that?? – Khalil Al Hooti Jan 11 '19 at 18:38
  • @KhalilAlHooti by design, the Qt developers did not see it necessary to show all the shorcuts, only the first. – eyllanesc Jan 11 '19 at 18:41
  • Ahhaaa, interesting, thanks telling me. I spent hours trying to fix it. @eyllanesc is there a way to show all shortcuts?? – Khalil Al Hooti Jan 11 '19 at 19:27