Unsolved Exposing my c++ function to a Python script
-
Hello all,
Is there a way to expose c++ functions/objects to a python script and evaluate this script in my qt program, just like whats done with the java script engine?
Thanks.
-
@Ahmadinho Do you mean you want to embed Python script in your C++ app? Like this: https://docs.python.org/3/extending/embedding.html (not Qt related)?
-
Hello jsulm, thanks for your help, yes something like that, however assume I have a function called add in my c++ code, i want the python script to be able to use that function.
-
@Ahmadinho
It's not very clear to me which way round you want access, Python -> C++, C++ -> Python, both.... In any case, if you read the very first sentence of the link @jsulm gave you it states:The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it.
So did you read that far?
-
Hi JonB, I did and it didnt seem to have a way to expose c++ objects as well, isnt there a qt module like the java script one?
The main thing I want to do is, have for example in my c++ code the following: class A with function C D, in the python script i can initiate instances from Class A and call A.B and A.B.
-
@Ahmadinho What about using PyQt or PySide?
-
@Ahmadinho said in Exposing my c++ function to a Python script:
it didnt seem to have a way to expose c++ objects as well,
You can do a Google on
expose c++ object to python
. There are a few approaches. It may be interesting to read one person's comment from https://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html#wrapping-c-c-for-python [my bold]:When I write wrappers for C and C++ code, I usually provide a procedural interface to the code and then use Python to construct an object-oriented interface. I do things this way for two reasons: first, exposing C++ objects to Python is a pain; and second, I prefer writing higher-level structures in Python to writing them in C++.
-
@Ahmadinho I think pybind11 or swig can achieve what you want.
-
Thanks a lot @student, will look into them further.
-
cython is also an possibility to wrap c/c++ code in python and its easier to set up than swig.