繁体   English   中英

如何使用 QProcess 中的 bash 命令“which”

[英]How to use the bash command 'which' from QProcess

我是一名使用 Qt 的学生程序员,我似乎遇到了使用 QProcess 启动 bash 命令“which”以尝试收集应用程序安装地图的问题。 我有以下代码,我真的不知道我可能会错过什么。 我已经参考了QProcess 文档,但仍然无法弄清楚出了什么问题。

每次运行此代码时,都不会在指定目录中创建文件。 如果没有构建文件,应用程序将无法继续。

//datatypes
QProcess *findFiles = new QProcess();
QStringList arguments;
QStringList InstallationList;
QString program = "/bin/bash";
QString currentUsersHomeDirectory = QDir::homePath();
QString tmpScriptLocation = currentUsersHomeDirectory;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
//generate file with list of files found
tmpScriptLocation += ".whichBAScriptOutput";
arguments << QString(QString("which -a certainFile >> ") += tmpScriptLocation);
findFiles->setProcessEnvironment(env);
findFiles->start(program,arguments);
findFiles->waitForFinished();

位于/usr/bin/所以尝试改变路径..

编辑:您需要将QProcess的信号readyReadStandardOutput() 连接到您的插槽。 实际上,如果您查看QProcess继承自QIODevice的文档。 这意味着您可以执行以下操作:

while(canReadLine()){
   string line = readLine();
   ...
}

如果您已经在 Qt 中编写了客户端-服务器应用程序,我相信您已经重新整理了伪代码。

正如您所说,您想执行which ,但是您正在使用手写脚本启动bash 有一种更简单的方法可以按顺序执行此操作:

//preparing the job, 
QProcess process;
QString processName = "which"; //or absoute path if not in path
QStringList arguments = QStringList() << "-a" 
                                      << "certainFile.txt";

// process.setWorkingDirectory() //if you want it to execute in a specific directory

//start the process                                  
process.start(processName, arguments ); 
//sit back and wait                                     
process.waitForStarted(); //blocking, return bool
process.waitForFinished(); //blocking, return bool                                      
if(process.exitCode() != 0){
  //Something went wrong
}

//return a byte array containing what the command "which" print in console
QByteArray processOutput = process.readAll(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM