'How to disable output of QProcess
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;
}
Solution 1:[1]
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 is SeparateChannels, 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adriaan de Groot |
