QProcess在运行python脚本之前被杀死

时间:2019-01-16 19:20:43

标签: c++ qt

对于一个学校项目,我与Pygame进行了乒乓球比赛。这一切都很好。现在,我正在Qt Creator的帮助下构建一个UI,以在GUI中执行我的游戏。

Qt Calling External Python ScriptQt process getting automatically killed等线程的帮助下,我为自己尝试了一切,但似乎并没有使我摆脱错误

“ QProcess:在进程(“ python”)仍在运行时被销毁。”

这是我要运行的脚本:

def test():
   for x in range(100):
     print(x)
print("python code !!!!")

test()

这是我按下按钮时的代码:

void SettingsWindow::on_pushButton_2_clicked()
{
QProcess process;
QString scriptFile =  QCoreApplication::applicationDirPath() + "/Users/wouterlefebvre/GithubRepos/Hello.py";

QString pythonCommand = "python " + scriptFile;

process.start (pythonCommand);
QProcess *myProcess = new QProcess();
myProcess->startDetached("python.exe     "/Users/wouterlefebvre/GithubRepos/Hello.py" );

myProcess->start();

//check that the process actually starts
if (!myProcess->waitForStarted()) {
    qDebug("Could not start process");
    return;
}


QTime time;
time.start();
//wait 4 seconds
while (time.elapsed() < 4000) {
    //keep the GUI working
    QApplication::processEvents();
}

myProcess->kill();
// wait for the process to actually stop
myProcess->waitForFinished();

delete myProcess;

}

据我了解,Qprocess在调用python脚本后停止了。但我不知道如何解决此问题。

我们将不胜感激。

编辑:使用此示例代码,我完成了工作。但是现在我在QT中有pygame的导入模块错误。知道为什么吗?

void SettingsWindow::on_pushButton_2_clicked()
{
QProcess p;
    QStringList params;
    QString pythonPath = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6";
    QString pythonScript = "/Users/wouterlefebvre/GithubRepos/Hello.py";

    params << pythonScript;
    p.start(pythonPath, params);
    p.waitForFinished(-1);
    QString p_stdout = p.readAll();
    QString p_stderr = p.readAllStandardError();
    if(!p_stderr.isEmpty())
       qDebug()<<"Python error:"<<p_stderr;
    qDebug()<<"Python result="<<p_stdout;
}

0 个答案:

没有答案