PDA

View Full Version : Run executable from within C++/Qt



timmu
3rd January 2013, 08:32
I'd like to run another program (a windows exe file) from within my C++/Qt code.

I was wondering if Qt offers anything to that effect and how to catch the results (a line of text)
so that my own code could continue using it?

I want to run "ent.exe -file myfile -f"

Thanks!

Santosh Reddy
3rd January 2013, 08:37
You can use QProcess (http://qt-project.org/doc/qt-4.8/qprocess.html) and capture the stdout channel of the process to get the results

timmu
3rd January 2013, 09:00
Dear Santosh,

Thank you for your help.
The following code compiled and executed:



QObject *parent;
QString program = "./ent";
QStringList arguments;
arguments << "-b" << "-t" << "input.txt";

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


1. However I now need to catch the results into a QString. What is the best way to do that?

2. Also, can I be certain that the rest of the program waits before this process is executed and
what happens if some error happens. How to catch an error?

Thanks!

Santosh Reddy
3rd January 2013, 09:39
1. However I now need to catch the results into a QString. What is the best way to do that?

2. Also, can I be certain that the rest of the program waits before this process is executed and
what happens if some error happens. How to catch an error?



QString program = "./ent";
QStringList arguments;
arguments << "-b" << "-t" << "input.txt";

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

if(myProcess->waitForStarted(5000))
{
QByteArray out_data = myProcess->readAllStandardOutput();
QString out_string(out_data);
qDebug() << out_string.toStdString().c_str();
}
else
{
qDebug() << "myProcess did not start in time";
}


This is just one simple way. You could use signal/slots also, please refer Qt Documentation for QProcess (http://qt-project.org/doc/qt-4.8/qprocess.html), it describes much of what you want

timmu
3rd January 2013, 10:01
Dear Santosh,

Thanks again.
The below code makes a QString that is empty as this does not print anything on the screen



QObject *parent;
QString program = "./ent";
QStringList arguments;
arguments << "-b" << "-t" << "input.txt";

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


if(myProcess->waitForStarted(5000))
{
QByteArray out_data = myProcess->readAllStandardOutput();
QString out_string(out_data);
qDebug() << out_string.toStdString().c_str();
cout << "results: " << out_string.toLocal8Bit().constData() << "\n";
}
else
{
qDebug() << "myProcess did not start in time";
}


I know that the other program works because this gives me the correct answer:


system("./ent -b -t input.txt");

Do you advise against using the latter version?

Santosh Reddy
3rd January 2013, 10:08
Try this

myProcess->setReadChannel(QProcess::StandardOutput);
myProcess->waitForReadyRead();
myProcess->readAllStandardOutput();

timmu
3rd January 2013, 10:21
Dear Santosh,

Thanks a lot. This beautifully solved it and everything works well now.




QObject *parent;
QString program = "./ent";
QStringList arguments;
arguments << "-b" << "-t" << "input.txt";

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

if(myProcess->waitForStarted(5000))

{

myProcess->setReadChannel(QProcess::StandardOutput);
myProcess->waitForReadyRead();

QByteArray out_data = myProcess->readAllStandardOutput();
QString out_string(out_data);
qDebug() << out_string.toStdString().c_str();
cout << "results: " << out_string.toLocal8Bit().constData() << "\n";
}

else
{
qDebug() << "myProcess did not start in time";
}


My last question is about this:



if(myProcess->waitForStarted(5000))


I'm assuming it means that the system waits for 5000 ms. Does it wait starting from when the external process was started or when it finshed?

Santosh Reddy
3rd January 2013, 10:58
It waits from the time the waitForStarted() call is made to time when the external process started. If the process is already started waitForStarted() will return right away.