How to start a Shell Script with QProcess?

守給你的承諾、 提交于 2020-01-21 11:43:39

问题


How can I start a Shell Script using QProcess? The Shell Script has eight different commands in it, some with arguments others without.

I tried to start the Shell Script with (using Ubuntu 11.10):

QProcess *Prozess = new QProcess();
Prozess->setWorkingDirectory(MainDirectory);
Prozess->start("/bin/sh", QStringList() << "Shell.sh");

But this doesn't work, that means nothing happens. How to make it work?


回答1:


Code is fine. Problem is at run-time.

Either your program can't run /bin/sh for some reason (test if you can run gedit instead?), or the MainDirectory variable has wrong directory path (debug it), or the Shell.sh does not exist in that directory (capitalization mistakes? What about "./Shell.sh"?), or you don't have enough privileges to run or modify target directory/files (are they owned by you?).




回答2:


The process you have started is running in background. if you want to see any explicit output from the running script you have to connect to void readyReadStandardOutput() or/and void readyReadStandardError() and read from the process explicitly. For example:

void onReadyRead() {

   QByteArray processOutput = Prozess->readAllStandardOutput();
}



回答3:


This should work:

QProcess::ProcessError Error = myProcess->readAllStandardError();
return Error;


来源:https://stackoverflow.com/questions/9086771/how-to-start-a-shell-script-with-qprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!