如何使用 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();
相关文章:
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 使用QProcess执行命令,并将结果存储在QStringList中
- 数组长度,为什么从命令行获取时不能使用它?
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- clang整洁10忽略了我的NOLINT命令
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 如何使用ESP8266向谷歌主页发送命令
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 通过 Openssl 命令行加密,通过 c++ 解密
- CMake "--target install"无需"--build"命令行
- Gstreamer 管道从命令 lne 到 c 代码
- 自动"yes"到 Popen SSH 命令
- 如何使用 QProcess 中的 bash 命令'which'