foxgod |
2019-11-26 17:11 |
为什么qt 调用python脚本,里面的print函数,有时候可以在qt控制台上打印出来,有时候不可以呢??
代码如下- Py_Initialize();
if ( !Py_IsInitialized() ) { return -1; } // 将Python工作路径切换到待调用模块所在目录,一定要保证路径名的正确性 PyRun_SimpleString("import sys"); QString setSysPath = QString("sys.path.append('%1')").arg(QCoreApplication::applicationDirPath()); PyRun_SimpleString(setSysPath.toStdString().c_str());
PyObject* pModule = PyImport_ImportModule("server"); // 这里的test_py就是创建的python文件 if (!pModule) { qDebug()<< "Cant open python file!\n" << endl; return -1; } PyObject* pFunhello= PyObject_GetAttrString(pModule,"usps"); // 这里的usps就是python文件定义的函数 if(!pFunhello) { qDebug()<<"Get function hello failed"<<endl; return -1; } else {
PyObject_CallFunction(pFunhello,NULL); while(true) { PyObject* pFunhello1= PyObject_GetAttrString(pModule,"udp_msglist"); // 这里的hellow就是python文件定义的函数
if(!pFunhello1) { qDebug()<<"Get function udp_msglist failed"<<endl; return -1; } PyObject* pReturn= PyObject_CallFunction(pFunhello1,NULL); char* cc=NULL; int result=-1; if(pReturn==NULL) { qDebug()<<"cc is null"; } else { //PyArg_Parse(pReturn,"s",&cc); PyArg_ParseTuple(pReturn,"s|i",&cc,&result); if(!QString(QLatin1String(cc)).isEmpty()) { qDebug()<<"cc is"<<QString(QLatin1String(cc))<<result; } }
} }
调用附件里面的py文件,print输出的log,怎么样可以在QT应用程序输出里面看到呢??,我实验了集中程序,控制台程序可以答应出来,窗口程序就打印不出来,什么原因[attachment=21107]
|
|