PyQt6 / Confusion over enum and flags

John F Sturtz john at sturtz.org
Wed Dec 18 05:38:10 GMT 2024


Hi again.

I am trying to port some PyQt5 code to PyQt6, and am running into 
confusion using the Qt.KeyboardModifierenum objects.  I think (hope) the 
answer to this question would probably enlighten me more generally as to 
what I'm misunderstanding:

Suppose I want to manually construct a QKeyCombination object.  This:

     QKeyCombination(Qt.KeyboardModifier.ControlModifier, key=65)

fails, telling me that argument 1 has unexpected type 
'KeyboardModifier'.

Indeed the Qt documentation for QKeyCombination says that argument 1 
should be of type Qt::KeyboardModifiers (plural).  And that the type of 
Qt::KeyboardModifier is enum, and that of Qt::KeyboardModifiers (plural) 
is flags.

It also says 'The KeyboardModifiers type is a typedef for 
QFlags<KeyboardModifier>. It stores an OR combination of 
KeyboardModifier values.'

I think I understand (on some level) that the concept of QFlags in Qt is 
not used in PyQt (anyway, I can't find anything called QFlags in PyQt).  
But then I'm beating my head against the wall trying to figure out:  
What does the QKeyCombination constructor want for its first argument?

The PySide6 documentation for QKeyCombination (unhelpfully) says:
>__init__(modifiers[, key=Qt.Key_unknown])
>
>     Parameters:
>             modifiers – Combination of KeyboardModifier
>             key – Key

What sort of 'combination'?  In what way do I combine KeyboardModifier 
objects so as to satisfy QKeyCombination's demand for an object of type 
KeyboardModifiers (plural) as the first argument?

Thanks again!

/John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20241218/37ca6583/attachment.htm>


More information about the PyQt mailing list