【问题标题】:PythonQt doesn't print anythingPythonQt 不打印任何东西
【发布时间】:2016-08-29 20:35:57
【问题描述】:

我正在关注http://pythonqt.sourceforge.net/Examples.html 的示例,但 PythonQt 不会在控制台上打印任何内容。我执行了一个只打印hello 的脚本,但没有打印任何内容。

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

另一方面,如果我使用普通的 python 嵌入执行它,它可以工作并打印hello

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

有趣的是,如果我在Py_Initialize(); 之前添加PythonQt::init();,则不会再次打印任何内容。所以我假设PythonQt::init(); 对python 的控制台输出做了一些事情。它是否以某种方式重定向它?如何打印?

我使用的是 Qt 4.8.6、PythonQt 2.1 和 Python 2.7.6。

【问题讨论】:

    标签: python c++ qt python-embedding pythonqt


    【解决方案1】:

    在阅读https://sourceforge.net/p/pythonqt/discussion/631393/thread/33ad915c 之后,似乎PythonQt::init(); 确实将python 输出重定向到了PythonQt::pythonStdOut 信号。

    这是因为PythonQt::init() 声明默认设置RedirectStdOut

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

    所以现在可以了:

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

    或者,我可以连接信号:

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

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2016-12-11
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多