[PyKDE] Calling from C++

John Fabiani jfabiani at yolo.com
Wed Jan 26 00:45:12 GMT 2005


On Tuesday 25 January 2005 11:06, Jim Bublitz wrote:
> On Tuesday 25 January 2005 10:10, John Fabiani wrote:
> > Can someone provide a link or demo code calling Python KDE from C++.  I'm
> > wondering if I can write a program in Python and have the C++ code
> > control the opening and closing of the Python code.
>
> I don't have a specific link, but it's no different than embedding Python
> using any other Python modules. A place to start is the Python "Extending
> and Embedding"  and "Python/C API" docs that should come with your Python
> version. The "Programming Python" book has a good chapter, although my copy
> is pretty dated now.
>
> The basic procedure is that you need to instantiate a Python interpreter
> (there's a C call to do that). Then from C/C++ you can make the interpreter
> execute strings, load and execute modules, pass and return values, etc.
> using Python's C lib calls from your C++ code. Probably the trickiest parts
> are understanding how to handle Python objects (there are conversion
> functions in the Python lib) and making sure you handle reference counting
> correctly. Even threading shouldn't be too much of a problem using more
> recent Python versions and sip 4.
>
> The other thing that can be helpful with PyQt/PyKDE is the sip Python
> module (import sip), primarily the wrapinstance/unwrapinstance methods
> described in Section 9, which allow you easily convert arguments between
> C/C++ and Python on the Python side. That makes it pretty easy to create a
> Qt or KDE object in C++ and use it in Python, or vice versa.
>
> Jim
Thanks a bunch I think I understand.  Looks like it may be easy.
John




More information about the PyQt mailing list