[PyKDE] Calling from C++

Jim Bublitz jbublitz at nwinternet.com
Tue Jan 25 19:06:17 GMT 2005


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




More information about the PyQt mailing list