namespace problems with qt6 and QMetaType
Matt Newell
newellm at blur.com
Mon Jan 5 22:36:14 GMT 2026
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"
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.
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.
I'm willing to come up with a patch if it's likely to get accepted.
Thanks,
Matt Newell
More information about the PyQt
mailing list