pythonc++qtpython-embeddingpythonqt

PythonQt doesn't print anything


I'm following the examples at http://pythonqt.sourceforge.net/Examples.html, but PythonQt doesn't print anything on the console. I execute a script that just prints hello, but nothing gets printed.

PythonQt::init();
PythonQtObjectPtr context = PythonQt::self()->getMainModule();
context.evalScript("print 'hello'\n");

On the other hand, if I execute it using plain python embedding it works and hello is printed:

Py_Initialize();
PyRun_SimpleString("print 'hello'\n");

What's interesting is that if I add PythonQt::init(); before Py_Initialize();, nothing gets printed again. So I assume PythonQt::init(); does something to python's console output. Does it redirect it somehow? How do I make it print?

I'm on Qt 4.8.6, PythonQt 2.1, and Python 2.7.6.


Solution

  • After reading https://sourceforge.net/p/pythonqt/discussion/631393/thread/33ad915c, it seems that PythonQt::init(); does redirect python output to the PythonQt::pythonStdOut signal.

    This is because PythonQt::init() declaration sets RedirectStdOut by default:

    static void init(int flags = IgnoreSiteModule | RedirectStdOut, const QByteArray& pythonQtModuleName = QByteArray());
    

    So this works now:

    PythonQt::init(PythonQt::IgnoreSiteModule);
    PythonQtObjectPtr context = PythonQt::self()->getMainModule();
    context.evalScript("print 'hello'\n");
    

    Or alternatively, I could connect the signal:

    QObject::connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), this, SLOT(Print(const QString&)));