Search code examples
pythonpyqt5migrationpyqt6

Migrate a QKeySequence from PyQt5 to PyQt6


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.


Solution

  • replace the + with a Pipe:

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

    or use a string:

    QKeySequence('Ctrl+S')