我想在Qt中执行一个外部程序,只需要返回代码。我不想在终端机上看到任何输出。我试图将stderr和stdout重定向到一个文件,但该命令的输出仍在屏幕上打印。
bool checkHostAlive(const QString& host, bool surpressOutput) {
QStringList parameters;
#if defined(WIN32)
parameters << "-n" << "1";
#else
parameters << "-c 1";
#endif
parameters << host;
auto proc = QProcess();
if(surpressOutput) {
// Surpress ping output
auto fileStdOut = QString();
auto fileStdErr = QString();
proc.setStandardErrorFile(fileStdErr);
proc.setStandardOutputFile(fileStdOut);
}
if (proc.execute("ping", parameters) == 0) {
return true;
}
return false;
}
发布于 2022-05-10 11:05:05
参见https://doc.qt.io/qt-5/qprocess.html#setStandardErrorFile,它建议nullDevice()
作为特殊值;或者,查看https://doc.qt.io/qt-5/qprocess.html#ProcessChannelMode-enum --默认情况是SeparateChannels
,它应该将输出发送给调用程序。
(请注意,用于非Windows的"-c 1"
可能是错误的;-c1
或字符串列表中的两个单独元素)
发布于 2022-07-04 13:06:31
我用这个:
this->gotDoxygen=QProcess::execute("sh",QStringList()<<"-c"<<"which doxygen 2>&1 >/dev/null");
要检查外部doxygen应用程序的存在,不显示任何输出。
https://stackoverflow.com/questions/72184505
复制相似问题