【问题标题】:How to disable output of QProcess如何禁用 QProcess 的输出
【发布时间】:2022-07-04 21:06:31
【问题描述】:

我想在 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; 
}

【问题讨论】:

  • QProcess::executestatic 成员,因此与实例 proc 关联的任何状态都无关紧要。此外,传递给setStandardErrorFile 等的参数。应该是一个有效的文件路径:我不确定空的 QString 在这里实际上会做什么。
  • @G.M.感谢您指出这些。我以为.func 不同于::func

标签: c++ qt qprocess


【解决方案1】:

参见例如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 或字符串列表中的两个单独元素)

【讨论】:

    【解决方案2】:

    我用这个: this->gotDoxygen=QProcess::execute("sh",QStringList()&1 >/dev/null"); 检查外部 doxygen 应用程序是否存在,不显示任何输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多