basin
basin

Reputation: 4190

QProcess disable stdout redirection

I want to start a process from a QT app and catch its termination event. Its done with the method QProcess::start(). But unlike in startDetached(), the standard output of the process is redirected to a buffer. I don't want that.
I can't find how to disable it. An ugly workaround is to call 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();
}

Upvotes: 0

Views: 2374

Answers (1)

Linville
Linville

Reputation: 3793

Does QProcess::closeReadChannel(ProcessChannel channel) work for you?

Closes the read channel channel. After calling this function, QProcess will no longer receive data on the channel. Any data that has already been received is still available for reading. Call this function to save memory, if you are not interested in the output of the process.

Like this-

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

Upvotes: 1

Related Questions