[PyQt] Connect PyQt signal to C++ slot

Jack Cosgrove jackcosgrove at gmail.com
Mon May 2 02:16:53 BST 2011


I will describe the general task I am performing beforehand as there may
be better way to do it than I am currently trying.

I am making a Python editor and console using Qt. This includes a
QScintilla editor and a QTextEdit for terminal output. I want to "pipe"
stdout and stderr from the Python interpreter to the terminal as the
Python program is executed by the interpreter.

I currently run the program using the Py_Initialize(),
PyRun_SimpleString(), Py_Finalize() paradigm. I am able to print stdout
and stderr to the console after the Python program has finished by
assigning a Python class to sys.stdout and sys.stderr which has a string
member that is later extracted using PyObject_GetAttrString after the
Python program finishes. This is done using PyRun_SimpleString() and a
bit of hard-coded Python in the C++ class invoking the Python
interpreter.

However I want output from the program to be appended to the terminal as
it is printed to stdout and stderr, not at the end of the program. My
idea for this was to make the Python class assigned to sys.stdout and
sys.stderr inherit from QObject, and emit a signal whenever output
occurs. I can emit this signal when output occurs with a QString
containing the message as an argument, but I don't know how to connect
the PyQt signal to a C++ slot from within the C++ class that is invoking
the Python interpreter.

Is this even possible? And if so, what functions (I suspect in sip.h)
should I use? It looks like I would use sipConvertFromNewType to wrap
the class with the slot with a PyObject, and then sipConnectRx to
connect the signal to the slot that appends to the terminal.

Thank you.



More information about the PyQt mailing list