[英]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. 我正在http://pythonqt.sourceforge.net/Examples.html上关注示例,但是PythonQt在控制台上不显示任何内容。 I execute a script that just prints hello
, but nothing gets printed. 我执行了一个只打印hello
的脚本,但没有打印任何内容。
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: 另一方面,如果我使用普通的python嵌入执行它,则可以正常工作并打印hello
:
Py_Initialize();
PyRun_SimpleString("print 'hello'\n");
What's interesting is that if I add PythonQt::init();
有趣的是,如果我添加PythonQt::init();
before Py_Initialize();
在Py_Initialize();
之前Py_Initialize();
, nothing gets printed again. ,则不会再打印任何内容。 So I assume PythonQt::init();
所以我假设PythonQt::init();
does something to python's console output. 对python的控制台输出有所帮助。 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. 我使用的是Qt 4.8.6,PythonQt 2.1和Python 2.7.6。
After reading https://sourceforge.net/p/pythonqt/discussion/631393/thread/33ad915c , it seems that PythonQt::init();
阅读https://sourceforge.net/p/pythonqt/discussion/631393/thread/33ad915c之后 ,似乎PythonQt::init();
does redirect python output to the PythonQt::pythonStdOut signal. 确实将python输出重定向到PythonQt :: pythonStdOut信号。
This is because PythonQt::init()
declaration sets RedirectStdOut
by default: 这是因为PythonQt::init()
声明默认设置RedirectStdOut
:
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&)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.