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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
public slots:
void btnpressed(void);
void prtstdout(void);
void prtstderr(void);
};
#endif // 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
To copy to clipboard, switch view to plain text mode
and dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
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());
}
#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());
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks