[C++-sig] Re: calling a python-callback... Qt & boost & python

Lars Kunert lkunert at mpi-sb.mpg.de
Sun Sep 28 23:46:08 CEST 2003


Hi!

It looks like I have finally solved my problem - despite there are some 
open questions.

To launch a Qt-application from python into its own thread and to 
communicate between them,
you can use the hippodraw-project strategy (like I have done)

The trick in their approach is
-to create a new thread first (using QThread)
-then create an empty QApp-Singelton on the new Thread (with no 
child's...)
-wait till the thread has completed doing this
and then add the children of the QApp from the main thread.

communication to the QtApp can be solves by wrapped methods...

communication from the QtApp into python can be solved by registered 
callbacks:
-at the time the new Thread is created, the interpreter-state of the old 
(main) thread can be accessed and used to create a new thread-state
-the thread state (a pointer) has to be saved as a static class pointer // 
I hope someone can tell me why a normal class pointer (provided, that the 
instance does not change) is not good enough.
>...
>  Thread *m_thread = new Thread();
>
>  m_thread->set_py_thread_state( PyThreadState_New( 
PyThreadState_Get()->interp ));
>  m_thread->start();
> ...
-the python-callback-function pointers can be stored in the QtApp by use 
of normal wrapped set-methods
-when the QtApp wants to use one of the callbacks it uses the stored 
threadstate to access the gil

> PyEval_RestoreThread( m_thread->py_thread_state());
>     boost::python::call<void>( _clicked_callback ); 
> m_thread->set_py_thread_state( PyEval_SaveThread());

I have used:
gcc version 3.3.1
boost 1.30.2
qt 3.1.1


OPEN QUESTIONS
- why do I have to use a static class pointer to hold the result of the 
"PyThreadState_New"-call ?
- how can I tell pyste (or gccxml) not to parse every included header - 
the problem is, that I had to include the boost/python-header into my 
python/c++ interface file


Thanks for your help,

Lars




More information about the Cplusplus-sig mailing list