如何禁用QProcess的输出

发布于 2025-01-27 17:13:52 字数 704 浏览 3 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2025-02-03 17:13:52

请参阅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 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)

故事灯 2025-02-03 17:13:52

我使用以下方式:

this->gotDoxygen=QProcess::execute("sh",QStringList()<<"-c"<<"which doxygen 2>&1 >/dev/null");

要检查是否存在外部Doxygen应用程序,但不会显示任何输出。

I use this:

this->gotDoxygen=QProcess::execute("sh",QStringList()<<"-c"<<"which doxygen 2>&1 >/dev/null");

To check for the presence of the external doxygen app, doesn't show any output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文