PDA

View Full Version : How to display the output of a process in a TextEdit widget in real time?



qtzcute
21st July 2009, 08:31
Hi,

I have an application that when run from shell, gives many messages, variable and their data as output in the same shell.

Now, i am running that application with the help of QProcess and i want the resulting ouput to display in my Text Edit widget of my GUI in real time....i.e. as the appication is producing output, it should be appearing in TextEdit concurrently.

I browse the google for the problem and found that people are using readLineStdout() which seems like not a slot provided by QProcess but they are using it as if it is.

spirit
21st July 2009, 08:48
all what you need it's to use QProcess::readyReadStandardOutput/QProcess::readyReadStandardError signals and create slots for these signals, then in these slots you should call QProcess::readAllStandardOutput/QProcess::readAllStandardError and add results in a text edit.

qtzcute
21st July 2009, 10:55
hmmm..right. I used following code but doesn't display anything in my TextEdit.


...
QObject::connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError(myProcess)));
QObject::connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText(myProcess)));
myProcess->start(program);


Definition of updateError and updateText:


void MainWindow::updateError(QProcess *myProcess)
{
QByteArray data = myProcess->readAllStandardError();
textEdit_verboseOutput->append(QString(data));
}

void MainWindow::updateText(QProcess *myProcess)
{
QByteArray data = myProcess->readAllStandardOutput();
textEdit_verboseOutput->append(QString(data));
}

spirit
21st July 2009, 11:11
sigh, you code won't work.
try this


connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
myProcess->start(program);
...
void MainWindow::updateError()
{
QByteArray data = myProcess->readAllStandardError();
textEdit_verboseOutput->append(QString(data));
}

void MainWindow::updateText()
{
QByteArray data = myProcess->readAllStandardOutput();
textEdit_verboseOutput->append(QString(data));
}

BUT, myProcess must be a class member of MainWindow or you can cast QObject::sender to QProcess.
PS, you should read more about Signal&Slot mechanism (http://doc.qtsoftware.com/4.5/signalsandslots.html).

qtzcute
21st July 2009, 11:47
Still not working. The process is however running fine but TextEdit widget remains empty.
My MainWindow class


class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
QProcess *myProcess;
MainWindow(QMainWindow *parent = 0);

private slots:
void updateError();
void updateText();
};


MainWindow.cpp


void MainWindow::On_pushButton_startAnalysis_Click()
{
QObject *parent;
myProcess= new QProcess(parent);

QString program = "./memviz ";
QCoreApplication::processEvents();
connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
myProcess->start(program);
}

void MainWindow::updateError()
{
QByteArray data = myProcess->readAllStandardError();
textEdit_verboseOutput->append(QString(data));
}

void MainWindow::updateText()
{
QByteArray data = myProcess->readAllStandardOutput();
textEdit_verboseOutput->append(QString(data));
}


Is some error still hiding there from my egle eyes? :mad:

spirit
21st July 2009, 11:53
what do you see in console when this code works?


void MainWindow::On_pushButton_startAnalysis_Click()
{
QObject *parent;
myProcess= new QProcess(parent);

QString program = "./memviz ";
QCoreApplication::processEvents();
qDebug() << connect(myProcess, SIGNAL(readyReadStdError()), this, SLOT(updateError()));
qDebug() << connect(myProcess, SIGNAL(readyReadStdOutput()), this, SLOT(updateText()));
myProcess->start(program);
}

void MainWindow::updateError()
{
QByteArray data = myProcess->readAllStandardError();
qDebug() << data;
textEdit_verboseOutput->append(QString(data));
}

void MainWindow::updateText()
{
QByteArray data = myProcess->readAllStandardOutput();
qDebug() << data;
textEdit_verboseOutput->append(QString(data));
}

qtzcute
21st July 2009, 12:10
Oh its giving following error


Object::connect: No such signal QProcess::readyReadStdError()
Object::connect: (receiver name: 'MainWindow')
Object::connect: No such signal QProcess::readyReadStdOutput()
Object::connect: (receiver name: 'MainWindow')

But this error should also have appeared in the texEdit_verboseOutput.

I said the program is working fine because i was actually generating an xml file with my 'memviz' application which is generated finely. Sorry din check the console earlier.

qtzcute
21st July 2009, 13:03
Any idea about this error? Isn't it weird QProcess::readyReadStdError() is a signal provided by QProcess as i see it in Qt Assistant but still i am getting 'no such signal error'.

spirit
21st July 2009, 13:20
take a look at docs: there are only these signals QProcess::readyReadStandardError && QProcess::readyReadStandardOutput.

wagmare
21st July 2009, 13:40
set the
void QProcess::setProcessChannelMode ( ProcessChannelMode mode )

to QProcess::MergedChannels

qtzcute
21st July 2009, 14:22
Ah...stupid me.

Thanks. It's working now.