more enum breakage

Phil Thompson phil at riverbankcomputing.com
Sun Jun 19 17:39:16 BST 2022


On 19/06/2022 10:18, Kovid Goyal wrote:
> On Sun, Jun 19, 2022 at 10:13:09AM +0100, Phil Thompson wrote:
>> On 19/06/2022 05:02, Kovid Goyal wrote:
>> > > From the gift that keeps on giving department:
>> >
>> > Returning Qt.ItemDataRole.TextAlignmentRole from the data() method of
>> > models does not work in PyQt6, one has to return an integer not an enum
>> > for it to work. So, for example:
>> >
>> >    return Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
>> >
>> > results in text rendered by Qt at top left corner, but
>> >
>> >    return (Qt.AlignmentFlag.AlignHCenter |
>> > Qt.AlignmentFlag.AlignVCenter).value
>> >
>> > works as expected.
>> 
>> It depends on wether we want the type of the value to be retained by
>> QVariant. With the current behaviour the following...
>> 
>> QVariant(Qt.AlignmentFlag.AlignHCenter).value() is
>> Qt.QAlignmentFlag.QAlignHCenter
>> 
>> ...is true.
>> 
>> If QVariant was changed to automatically convert an enum member to an 
>> int
>> then that type information would be lost. I'm not sure how important 
>> that
>> is???
> 
> Well, you could potentially want to get the current value of flags from
> a model and expect to get an enum as a result not an int. However,
> unless the model was implemented in Python that would not work anyway,
> so I dont think its too important. Perhaps you can just document
> QVariant as not preserving enums through a roundtrip. This is what the
> C++ version does as well, IIRC.

Thinking about it you should be able to get the int behaviour without it 
needing to be an int. I'll have a play.

Phil


More information about the PyQt mailing list