0

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

测试.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

测试.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();
}
4

1 回答 1

1

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

关闭读取通道通道。调用此函数后,QProcess 将不再接收通道上的数据。任何已经接收到的数据仍然可供读取。如果您对进程的输出不感兴趣,请调用此函数以节省内存。

像这样-

QProcess *process = new QProcess();
process->start("/bin/echo aaa");
process->closeReadChannel(QProcess::StandardOutput);
process->closeReadChannel(QProcess::StandardError);
于 2013-03-15T03:58:47.793 回答