Passing a CPP class to a Python method
Dynamic Python binding for Qt Applications
Brought to you by:
florianlink,
marcusbarann
Long story short, I have a python class
TypeProcessor
defined such that one of the methods takes aCustomType
CPP class. I've wrapped upCustomType
as per the PythonQt examples, and am having success simply calling other methods ofTypeProcessor
via calls to thecall
routine of the corresponding PythonQtObjectPtr. However, those methods are easy to call since they all take types that cast to QVariants out of the box, so I can do:So my question is simply how to pass my
CustomType
into the arguments list of the call routine. I get an error when I try:Any suggestions?
Any thoughts on this, folks? I'm tempted to just copy the data from my
CustomType
into a QJsonObject so I can pass that to Python, but it'd be much cleaner if I could avoid any unnecessary serialization.I am the 'folks'... Well, this is not a very common use case but there are probably multiple solutions... If your CustomType was derived from QObject, you could just create a Qvariant from that pointer. But since it is a C++ only type, you need to get it through the Qvariant without losing the type information. I think you could register a CustomType POINTER meta type, probably PythonQt would then know the type by the meta type id and since it is a registered C++ class, it should know what to do.
If that does not work, you can also use
in which case you do the conversion to Python yourself and pass the wrapper to your call.
Oh hello Florian! I guess you are the folks. Thanks a bunch for the suggestions, I'll be sure to try them out. Do you think PythonQt would recognize that I've wrapped the C++ class, à la
PyCPPWrapperExample
? If so, that'd be super swell.Thanks again.
Hi Florian, I can confirm that the wrapped class gets recognized. What a beautiful thing. Thanks so much!
The problem is QVariant, it can only transport registered meta types. So it
can transport a PyObject, a PythonQtObjectPtr and a QObject, but not a
custom C++ pointer, it would take it as void* and lose type information.
So you either convert it first to something that QVariant will handle, or
you register its pointer as a meta type, so that QVariant does not lose the
type information.
On Thu 5. Mar 2020 at 04:57, Flower lady fancyflowerlady@users.sourceforge.net wrote: