1

我是一名使用 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();
4

2 回答 2

2

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

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

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

如果你已经在 Qt 中编写了一个客户端-服务器应用程序,我相信你重新编译了伪代码..

于 2012-11-14T13:38:32.807 回答
0

正如您所说的要执行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(); 
于 2012-11-14T13:57:19.577 回答