PyQt6 QWidget.keyPressEvent() behaviour changed?

Marko Luther marko.luther at gmx.net
Tue Nov 16 16:14:49 GMT 2021


Dear Walter,

> On 16. Nov 2021, at 16:32, Baz Walter <bazwal at gmail.com> wrote:
> 
> On 16/11/2021 12:34, Marko Luther wrote:
>> with below example code I observe a difference between PyQt6 and PyQt5 in the result of the QKeyEvent delivered by the keyPressEvent method of QWidget.
>> While in PyQt5 the event.key() value did not change on pressing a modifier key together with another key, while in PyQt6 it does. Thus a simple comparison like event.key() == Qt.Key.Key_H does not work to recognise if the H key is pressed independent of modifier keys.
>> Is this intentional? How would one recognise the press of the H-key independent of any modifier?
> 
> On my Linux system, I see this:
> 
> # PyQt6 - Alt
> 72 h False True
> 16777251  True False
> 72 h True True
> 
> # PyQt5 - Alt
> 72 h False True
> 16777251  True False
> 72 h True True
> 
> # PyQt6 - AltGr
> 72 h False True
> 16781571  False False
> 294 ħ False False
> 
> # PyQt5 - AltGr
> 72 h False True
> 16781571  False False
> 294 ħ False False
> 
> Thus, when a modifier produces different text, a different key is returned. The same behaviour is seen with Shift - if I press e.g. Shift+5, the returned key is 37 (%), not 53 (5). The only exception to this is with upper/lower case letters, which always return the same key.
> 
> Given that, I would expect the character ª to return 170, not 72. In which case, the Qt6 behaviour on your system seems right to me, and the Qt5 behaviour seems wrong.
> 
> What specific versions of Qt5/6 are you using, and on what version of MacOS?

This is using the lastest PyQt5/6 from pip on macOS 11.6. I guess the PyQt5 result is correct as this delivers the modifier and the key-press separate. The key should  in the best case match the corresponding item in the enum Qt.Key. Thus the key() result is just an int and not an enum element. Very confusing. In any case this is an incompatibility between PyQt5 and PyQt6.

I wonder how best to resolve this. In my case I have to catch the H-key press, independent of any modifier key that might have pressed at the same time. Do I really have to check for all variants?

M






More information about the PyQt mailing list