PDA

View Full Version : QProcess can read stderr but no stdout



mastupristi
20th October 2010, 22:53
I tried to use QProcess. I had some problems with reading stdout, so I create a simple project to reproduce the problem:

suppose to have a simple program like this:

#include<stdio.h>
#include <unistd.h>

int main(void)
{
int o,e;
o = e = 0;

while(1)
{
printf("o %d\n", o);
usleep(200000);
fprintf(stderr,"e %d\n", e);
usleep(200000);

o++;
e++;
}

return 0;
}

that works fine. When I run it I have the following output on the console:
o 0
e 0
o 1
e 1
o 2
e 2
o 3
e 3
o 4
e 4
o 5
...

then I have a simple qt project based on QProcess:

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QProcess>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private:
Ui::Dialog *ui;
QProcess extpro;

public slots:
void btnpressed(void);
void prtstdout(void);
void prtstderr(void);
};

#endif // DIALOG_H


and dialog.cpp:

#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);

connect(ui->pushButton,SIGNAL(clicked()),this, SLOT(btnpressed()));

connect(&extpro, SIGNAL(readyReadStandardOutput()), this, SLOT(prtstdout()));
connect(&extpro, SIGNAL(readyReadStandardError()), this, SLOT(prtstderr()));
}

Dialog::~Dialog()
{
delete ui;
extpro.kill();
}


void Dialog::btnpressed(void)
{
extpro.start("../zozzo-build-desktop/zozzo");
}

void Dialog::prtstdout(void)
{
qDebug() << QString(extpro.readAllStandardOutput());
}

void Dialog::prtstderr(void)
{
qDebug() << QString(extpro.readAllStandardError());
}

If I try to execute obtain only:
"e 0
"
"e 1
"
"e 2
"
"e 3
"
"e 4
"
"e 5
"
"e 6
"
"e 7
"

I can see only stderr. How can I have also stdout to have an output just like console?

In attachment there is the projects

thanks

tbscope
21st October 2010, 04:43
I haven't looked at all your code but are you sure that

printf("o %d\n", o);
prints to stdout and not the current console/shell/...?

mastupristi
21st October 2010, 07:48
I haven't looked at all your code but are you sure that

printf("o %d\n", o);
prints to stdout and not the current console/shell/...?
of course
anyway try to change that line to:

fprintf(stdout,"o %d\n", o);

thanks

mastupristi
21st October 2010, 09:47
of course
anyway try to change that line to:

fprintf(stdout,"o %d\n", o);
I have just tried
Same behaviour :(