namespace problems with qt6 and QMetaType
Phil Thompson
phil at riverbankcomputing.com
Thu Jan 8 10:34:08 GMT 2026
On 05/01/2026 22:36, Matt Newell wrote:
> Pseudo example:
>
> .h
>
> namespace NS {
> class Record {...};
> };
> qRegisterMetaType<Record>("Record");
>
> .sip
>
> %ModuleHeaderCode
> #include "record.h"
> using namespace NS;
> %End
>
> class Record {...};
>
> This has always worked fine but the move to Qt6 removes the ability to
> set the name of the QMetaType when calling qRegisterMetaType. The
> name always ends up fully qualified with the namespace("NS::Record").
>
> Whenever a Record instance gets stored internally as a QVariant
> PyQt/sip is no longer able to convert it back to the correct python
> type instead giving an error:
>
> "TypeError: unable to convert a C++ 'NS::Record' instance to a Python
> object"
Just so I'm clear. The cause is restricted behaviour in Qt6 vs Qt5, but
otherwise Qt6 is behaving correctly?
> This is because PyQt/sip (Chimera::toAnyPyObject calls sipFindType)
> cannot find the fully qualified name. Since I can't seem to find a
> way to work around this at the Qt level, the only options are to
> either convert a ton of code to specify the namespace in the .sip
> files along with using %HideNamespace to avoid changes at the python
> level... or to get some PyQt/sip changes merged to handle this easier.
My initial reaction is to say use %HideNamespace as that's exactly what
it's designed for.
> The simplest fix would be to have Chimera::toAnyPyObject remove any
> namespace prefix if the previous call to sipFindType fails. Other
> solutions would be to extend sipFindType to find the type with or
> without the namespace, possibly configurable with a class annotation.
> Alternatively an annotation specifying the QMetaType name that could
> be used to map it to the sip type name.
Having sipFindType() make a series of guesses wouldn't be acceptable.
Otherwise the simplest solution I can think of (which may not work) is
to add a CppName class annotation, ie. '/CppName=NS::Record/'. That way
everything could be handled in the parser and not require any changes to
the generated code and related data structures.
Phil
More information about the PyQt
mailing list