PDA

View Full Version : [QProcess] Calling console program



bgarisn
24th February 2010, 23:35
Hello all,

First let me say that I've tried to look through these forums for an answer to my problem, but as of yet, have been unsuccessful, so here it goes.

I have a simple utility program that is command line based. I have been developing a GUI to get the required parameters and then pass them to the command line program when my "Process" button is clicked. Here is the code that is executed when the button is clicked:



QString program = "/home/bgarrison/code/myUtils";
QStringList arguments;

QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);

myProcess->waitForFinished();
QString strOut = myProcess->readAllStandardOutput();

qDebug() << strOut;


If I run 'myUtils' with no arguments, I should get "This is a test." on std::out. However, my debug print prints ""...

I'm at a loss for why this is so.

I've tried the examples in the documentation:



QString program = "./path/to/Qt/examples/widgets/analogclock";
QStringList arguments;
arguments << "-style" << "motif";

QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);


And it brings up the AnalogClock example just fine. I've even tried to call several of the other examples, which also worked.

Why can I not call my command-line based program?

Thanks for any and all advice.

navi1084
25th February 2010, 04:41
try to use the signal readyReadStandardOutput () and in slot get the data.

wagmare
25th February 2010, 06:31
navi(avatar) is right ... u can get the readAllStandardOutput() or readAllStandardError() only after the QProcess emits readyRead() signal .. be sure u are receiving std output or std error ... so wait till u receives the signal readyReadStandardOutput() or readyReadStandardError() ..

bgarisn
25th February 2010, 15:11
Ok, I still am doing something wrong, but I took the advice both of you presented me. When I click my button, which invokes my command-line based program, it does the following:



QString program = "/home/bgarrison/code/myUtils";
QStringList arguments;

myProcess = new QProcess(this);

connect (myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(printOutput()));
connect (myProcess, SIGNAL(readyReadStandardError()), this, SLOT(printError()));

myProcess->start(program, arguments);

myProcess->waitForFinished();


printOutput() is a private SLOT in my class and it does the following:



void cMyUtils::printOutput()
{
ui->e_Log->append("Got to printOutput()"); // TextEdit to see results

QByteArray byteArray = myProcess->readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");

foreach (QString line, strLines){
ui->e_Log->append(line);
}
}




void Dialog_FileSplitter::printError()
{
ui->e_Log->append("Got to printError()");

QByteArray byteArray = myProcess->readAllStandardError();
QStringList strLines = QString(byteArray).split("\n");

foreach (QString line, strLines){
ui->e_Log->append(line);
}
}


This resulted in me being able to see why the program was not starting :)



Got to printError()
/home/bgarrison/code/myUtils: error while loading shared libraries: libboost_regex.so.1.41.0: cannot open shared object file: No such file or directory


Looks like I have a problem linking the Boost libraries, not calling the program using QProcess. Thanks for all of your help!