PDA

View Full Version : QProcess...what's wrong



nupul
4th May 2006, 07:44
I just can't get the output of "GNU locate" onto the Textedit Widget. Infact, the QByteArray is being detected empty!!

if i start the process in startDetached(...) I can see the output on my terminal! but using only start(...) I can't seem to get it working!!

Help pls...



#include <QApplication>
#include <QString>
#include <QWidget>
#include <QLabel>
#include <QProcess>
#include <QTextEdit>
#include <QTextStream>
#include <QByteArray>

int main(int argc,char *argv[])
{
QApplication app(argc,argv);

QWidget *w=new QWidget(0);

w->setGeometry(0,0,300,300);

QTextEdit *output=new QTextEdit(w);

QProcess locate;
locate.setReadChannel(QProcess::StandardOutput);
locate.start("locate *.mymenu");

QByteArray results=locate.readAllStandardOutput(); //readAll() too didn't work

if(results.isEmpty())
{
w->setWindowTitle("Sorry...doesn't work!");
}

QTextStream data(&results);

output->setPlainText(data.readAll());

w->show();

return app.exec();
}



:confused:

Thanks

jpn
4th May 2006, 08:49
You'll have to either connect to signal QProcess::readyReadStandardOutput() (http://doc.trolltech.com/4.1/qprocess.html#readyReadStandardOutput) to get notified when the data is available to be read, or more simply just wait for the whole process to finish before you read:

locate.waitForFinished(-1);

nupul
4th May 2006, 11:24
IT WORKS!!!! :D how stupid of me to overlook this....even after having read the docs!!

Now w.r.t to the first technique of using readyReadStandardOutput() it'll go something like this right?




QObject::connect(locate,SIGNAL(readyReadStandardOu tput()),myobject,SLOT(read_data()));



Is this correct?

Nupul

jpn
4th May 2006, 12:08
Yes, something like that. ;)
Just keep in mind that in some cases the signal might be emitted several times.

1) process has output something and readyReadStandardOutput is emitted (while the process is still continued)
2) you read the std output
3) new data has arrived to the std output, the signal is emitted again..
...