On 13Apr2020 2105, Chris Meyer wrote:
How would I call a Python function from the C++ application that returns
a Python object to C++ and then call a method on that Python object from
C++?
My specific example is that I create Python handlers for Qt windows and
then from the Qt/C++ I call methods on those Python objects from C++
such as “handle mouse event”.
You're in a bit of trouble here regardless, depending on how robust you
need to be. If you've only got synchronous, single-threaded event
handlers then you'll be okay. Anything more complex and you'll have some
fun debugging sessions to look forward to.
I would definitely say look at PyBind11. A while ago I posted a sample
using this to embed Python in a game engine at
https://devblogs.microsoft.com/python/embedding-python-in-a-cpp-project-with-visual-studio/
(VS is not required, it just happened to be the hook to do the
post/video ;) )
To jump straight to the code, go to
https://github.com/zooba/ogre3d-python-embed/blob/master/src/PythonCharacter.cpp
and search for "py::", and also
https://github.com/zooba/ogre3d-python-embed/blob/master/src/ogre_module.h
PyBind11 is nice for avoiding the boilerplate and ref-counting, but has
its own set of obscure error cases. It's also not as easy to debug as
Cython or going straight to the Python C API, depending on what the
issue is, as there's no straightforward generated code. Even stepping
through the templated code interactively in VS doesn't help make it any
easier to follow.
Cheers,
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/GVXH2AJC7Z2F5AIBIMCXEDKXLEYVCCU4/
Code of Conduct: http://python.org/psf/codeofconduct/