[PyQt] Getting parent class for an enum

Phil Thompson phil at riverbankcomputing.com
Sun Jun 10 12:37:47 BST 2018


On 17 May 2018, at 2:48 pm, Phil Thompson <phil at riverbankcomputing.com> wrote:
> 
> On 17 May 2018, at 2:11 pm, Kyle Altendorf <sda at fstab.net> wrote:
>> On 2018-05-17 04:55, Phil Thompson wrote:
>>> On 17 May 2018, at 9:38 am, Florian Bruhin <me at the-compiler.org> wrote:
>>>> On Thu, May 17, 2018 at 09:23:56AM +0100, Phil Thompson wrote:
>>>>> On 16 May 2018, at 1:57 pm, Denis Rouzaud <denis.rouzaud at gmail.com> wrote:
>>>>>> Hi,
>>>>>> In the case of an enum (with Q_ENUM macro), it would be interesting to have a way to get the parent class to be able to get the meta enum from the enum value (enumValue):
>>>>>> base = type(enumValue).parentClass()
>>>>>> idx = base.staticMetaObject.indexOfEnumerator(type(enumValue).__name__)
>>>>>> metaenum = base.staticMetaObject.enumerator(idx)
>>>>>> Would it be possible to add this capability to sip.enumType ?
>>>>>> Either to return the parent class or directly the metaEnum?
>>>>> Are you asking for an implementation of QMetaEnum.fromType()?
>>>>> For example...
>>>>>  metaenum = QMetaEnum.fromType(type(Qt.Key_F1))
>>>> That'd be useful indeed, IMHO!
>>>>>> Phil, how complicate is it?
>>>>>> Do you have any interest/plan to do it...?
>>>>> It shouldn't be too difficult (famous last words). This area needs an update anyway to add support for Q_ENUM(), Q_FLAG() and Python enum objects.
>>>> Oh, if PyQt enums would be a subclass of Python's enum.Enum (or
>>>> enum.IntEnum) objects - that'd be nice! :)
>>> C++ scoped enums are implemented as Python enums, but there are very
>>> few of them in Qt at the moment. Changing the implementation of
>>> traditional enums would (IMHO) cause compatibility issues.
>> 
>> Deprecate the classic implementation for 'awhile' and also provide them all as Python enums?  I'll also note that type(MyEnum.a_value) -> MyEnum so that part is easy with Python enums.
> 
> A member of a scoped enum is only exposed as an attribute of the enum.
> 
> A member of a traditional enum has the same visibility as the enum itself.
> 
> Changing the implementation would mean changing any code that used any enum member.
> 
> Switching to Python enums and also exposing the emum members (to maintain compatibility) seems pointless to me.

What I've actually done is to make the members of traditional enums visible from within the scope of the enum (as well as being visible from the same scope as the enum).

This means that you *should* change your code to use the scope (ie. Qt.Key.Key_F1 rather than Qt.Key_F1) but you don't *have* to. However you will have to for PyQt6.

Phil


More information about the PyQt mailing list