[C++-sig] calling a python-callback-function from a c++thread...

Niall Douglas s_sourceforge at nedprod.com
Sat Sep 27 18:51:58 CEST 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 26 Sep 2003 at 19:23, Lars Kunert wrote:

> PyApp.* wraps QtApplication by exporting selected methods to pyton,
> launches the thread, QtApp lives on, and locks/unlockes (syncs)
> accesses to the internal state of QtApp. PyApp.h

You cannot properly run Qt GUI functions from ANY thread other than 
the primary one. This is a Qt limitation.

You *can* run python in other threads, but it may not call any code 
in Qt which uses a GUI class. You could use the Qt global lock but I 
recommend you don't and recode your app instead to invoke GUI 
functions by sending APC messages to the GUI loop. I have written 
large heavily multithreaded apps with Qt and the global lock system 
creates more problems (deadlocks) than it solves.

> QtApp.* is a "normal"QtApplication, which tries to use a callback and
> dies... QtApp.h:
> [snip]
> void  QtApp::callback() 
> {
>     boost::python::call<void>( _callback );
> };

Why aren't you locking python before you enter it? If Qt and python 
run in two different threads, you must do this.

> >>> from PyApp_module import App
> >>> a=App()
> >>> dir(a)
> ['__class__', '__delattr__', '__dict__', '__doc__',
> '__getattribute__', '__hash__', '__init__', '__instance_size__',
> '__module__', '__new__', '__reduce__', '__repr__', '__setattr__',
> '__str__', '__weakref__', 'exec', 'set_callback', 'set_text', 'text']
> >>> def ph(): ...     print( "hallo" ) ... >>> ph() hallo >>>
> a.set_callback( ph ) <----now I invoke the callback (by pressing a
> button on the application window) >>> Segmentation fault

You also appear to not be initialising Qt by creating an instance of 
QApplication at the start before calling any other Qt code.

> I hope the preceeding code-snips will help to explain the situation
> sufficiently. If there are requests, I can also post the complete
> test-application...

How are you proposing to implement signals and slots in python?

Cheers,
Niall





-----BEGIN PGP SIGNATURE-----
Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2

iQA/AwUBP3XALsEcvDLFGKbPEQIrWgCdGJX9WZvur6kFwSvxhIFETozck2UAnR+R
fCtUtNIFoIz3WtmK6dFPFTrU
=WNda
-----END PGP SIGNATURE-----




More information about the Cplusplus-sig mailing list