Skip to content

Commit c35d010

Browse files
usiemsmrbean-bremen
authored andcommitted
Add "enum class" values to the enum type in Python
Enum class values seem to have pretty generic names (as there is no danger of name clashes in C++, as they must always be prefixed with the enum class name). So add them to the enum type in Python too, so one can always write something like QActionGroup.ExclusionPolicy.None instead of QActionGroup.None. Ideally we wouldn't add the enum values to the parent class at all, but I don't want to break compatibility with older versions of PythonQt.
1 parent c9cc51f commit c35d010

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/PythonQtClassInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,26 @@ PyObject* PythonQtClassInfo::findEnumWrapper(const QByteArray& name, PythonQtCla
853853
}
854854
}
855855

856+
// required for the code below (for versions before Qt 6)
857+
Q_DECLARE_METATYPE(PythonQtObjectPtr)
858+
856859
void PythonQtClassInfo::createEnumWrappers(const QMetaObject* meta)
857860
{
858861
for (int i = meta->enumeratorOffset();i<meta->enumeratorCount();i++) {
859862
QMetaEnum e = meta->enumerator(i);
860863
PythonQtObjectPtr p;
861864
p.setNewRef(PythonQtPrivate::createNewPythonQtEnumWrapper(e.name(), _pythonQtClassWrapper));
865+
#if QT_VERSION > 0x050800
866+
if (e.isScoped()) {
867+
// add enum values to the enum type itself, in case enum value names are so generic
868+
// that they are not unique
869+
for (int j = 0; j < e.keyCount(); j++) {
870+
PythonQtObjectPtr enumValuePtr;
871+
enumValuePtr.setNewRef(PythonQtPrivate::createEnumValueInstance(p.object(), e.value(j)));
872+
p.addVariable(e.key(j), QVariant::fromValue(enumValuePtr));
873+
}
874+
}
875+
#endif
862876
_enumWrappers.append(p);
863877
}
864878
}

0 commit comments

Comments
 (0)