-1

I'm in the middle of migrating a quit simple GUI application from PyQt5 to PyQt6. I'm stuck on the following problem about combining Keys and Modifiers.

The original PyQt5 code looked like this

self.button.setShortcuts(QKeySequence(Qt.CTRL + Qt.Key_S))

The "location" of CTRL and Key_S was moved and now need to look like this.

self.button.setShortcuts(QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_S))

But now I have the problem that the + doesn't work here anymore.

    self.button.setShortcuts(QKeySequence(Qt.Modifier.CTRL + Qt.Key.Key_S))
                                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'Modifier' and 'Key'

It is not easy for me to read the PyQt documentation because there is no such documentation. It is just autogenerated "docu" derived from its C++ version. It is not Python specific and hard to "transfer" to Python use cases. I don't know where to look.

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • As the [documentation shows](https://doc.qt.io/qt-6/qkeysequence.html#details) the plus sign is deprecated. Use `|` instead (or strings, like `"Ctrl+s"`). – musicamante May 18 '23 at 15:13
  • The docu you are linking to is C++ not Python. There is C++ code. – buhtz May 18 '23 at 18:26
  • 99% of the time, you can use the c++ docs. There's nothing python specific about using the | or the string. – mahkitah May 18 '23 at 18:31
  • 1
    Even the QKeySequence isn't needed: `button.setShortcut('Ctrl+S')` will automatically cast the string. – ekhumoro May 18 '23 at 18:37
  • @buhtz That's irrelevant: logical operators (including bitwise ones) work in the same way in Python as in most major programming languages, including C++. – musicamante May 18 '23 at 19:12
  • `|` is not "logical" but a "bitwise" operator which is possible but unusual in Python. – buhtz May 19 '23 at 12:46
  • 1
    @buhtz Yes, sorry; you're right, and I mistyped that: I meant "most operators". But, no, it certainly is ***not*** unusual in Python: those operators are not only fundamental in many SL aspects (like Enum), but they were also introduced (at least, AFAIK) since Python2 and largely used. And, honestly, if you're so aware about terminology differences, pointing out that those docs refers to C++ and not Python makes it even more irrelevant. Besides, there's **no** completely reliable Qt documentation for Python, including the official "Qt for Python" one, which is often incomplete and misleading. – musicamante May 22 '23 at 02:07

1 Answers1

1

replace the + with a Pipe:

QKeySequence(Qt.Modifier.CTRL | Qt.Key.Key_S)

or use a string:

QKeySequence('Ctrl+S')
mahkitah
  • 562
  • 1
  • 6
  • 19
  • Great. Does a function like `setShortcuts()` also accept strings? `button.setShortcuts(['F1', 'Q', QKeySequence('Ctrl+U')])` – buhtz May 18 '23 at 18:38
  • 1
    @buhtz Why not just try it and find out? (Spoiler Alert: yes, of course it does). – ekhumoro May 18 '23 at 18:42
  • 1
    @buhtz QAbstractButton only allows setting just a single shortcut (there is only a [`setShortcut()`](//doc.qt.io/qt-5/qabstractbutton.html#shortcut-prop) function, in singular form): every time you call `setShortcut()` more than once, the previous shortcut will be "released". If you want to use multiple shortcuts for the same widget, use multiple [QShortcuts](https://doc.qt.io/qt-5/qshortcut.html) and connect their `activated` signal to the function you want to call (probably, [`click()`](//doc.qt.io/qt-5/qabstractbutton.html#click)). – musicamante May 19 '23 at 00:20