如何禁用QProcess的输出
我想在QT中执行外部程序,然后获得返回代码。我不想在终端中看到任何OUPUT。我试图将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;
}
I want to execute an external program in Qt and just get the return code. I do not want to see any ouput in the terminal whatsoever. I tried to redirect stderr and stdout to a file but the output of the command is still being printed on screen.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅Eg https://doc.qt.io/qt-5/qprocess。 html#setStandArdErrorfile ,它建议
nulldevice()
作为特殊值; alternately, look into https://doc.qt.io/qt- 5/qprocess.html#processChannelMode-enum - 默认值为sapeaTechannels
,该应该将输出发送到调用程序。(请注意,非Windows的
“ - C 1”
是可能错误;-c1
或StringList中的两个单独的元素)See e.g. https://doc.qt.io/qt-5/qprocess.html#setStandardErrorFile , which suggests
nullDevice()
as special value; alternately, look into https://doc.qt.io/qt-5/qprocess.html#ProcessChannelMode-enum -- the default isSeparateChannels
, which should send output to the calling program.(Note that the
"-c 1"
for non-Windows is probably wrong; either-c1
or two separate elements in the stringlist)我使用以下方式:
要检查是否存在外部Doxygen应用程序,但不会显示任何输出。
I use this:
To check for the presence of the external doxygen app, doesn't show any output.