QProcess禁用标准重定向

时间:2013-03-15 02:41:37

标签: qt

我想从QT应用程序启动一个进程并捕获其终止事件。它完成了方法QProcess :: start()。但与startDetached()不同,进程的标准输出被重定向到缓冲区。我不希望这样 我找不到如何禁用它。一个丑陋的解决方法是调用setStandardOutputFile(“/ dev / stdout”)

test.h

#ifndef MY_TEST_H
#define MY_TEST_H

#include <QCoreApplication>

class MyApp : public QCoreApplication {
  Q_OBJECT  
  private Q_SLOTS:
    void subprocessStarted();
    void subprocessFinished(int);
  public:
    MyApp( int & argc, char ** argv );
};

#endif

TEST.CPP

#include "test.h"

#include <QProcess>
#include <QCoreApplication>
#include <stdio.h>

#define ENTRY printf("%s\n", __PRETTY_FUNCTION__)

MyApp::MyApp( int & argc, char ** argv ) : QCoreApplication(argc, argv) {
  ENTRY;

  QProcess *process = new QProcess();
  //process->setStandardOutputFile("/dev/stdout");
  process->start("/bin/echo aaa");
  bool b;
  b = connect(process, SIGNAL(started()), SLOT(subprocessStarted()));
  printf("connect started %d\n", b);
  b = connect(process, SIGNAL(finished(int)), SLOT(subprocessFinished(int)));
  printf("connect finished %d\n", b);
}
void MyApp::subprocessStarted() {
  ENTRY;
}
void MyApp::subprocessFinished(int ret) {
  ENTRY;
  printf("%d\n", ret);
}

int main(int argc, char *argv[]) {
  ENTRY;
  MyApp a(argc, argv);
  return a.exec();
}

1 个答案:

答案 0 :(得分:1)

QProcess::closeReadChannel(ProcessChannel channel)是否适合您?

  

关闭读取通道通道。调用此函数后,QProcess   将不再接收频道上的数据。任何已经存在的数据   已收到仍可供阅读。将此功能称为   如果您对过程的输出不感兴趣,请保存内存。

喜欢这个 -

QProcess *process = new QProcess();
process->start("/bin/echo aaa");
process->closeReadChannel(QProcess::StandardOutput);
process->closeReadChannel(QProcess::StandardError);