Run executable from within C++/Qt
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!
Re: Run executable from within C++/Qt
You can use QProcess and capture the stdout channel of the process to get the results
Re: Run executable from within C++/Qt
Dear Santosh,
Thank you for your help.
The following code compiled and executed:
Code:
arguments << "-b" << "-t" << "input.txt";
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!
Re: Run executable from within C++/Qt
Quote:
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?
Code:
arguments << "-b" << "-t" << "input.txt";
myProcess->start(program, arguments);
if(myProcess->waitForStarted(5000))
{
QByteArray out_data
= myProcess
->readAllStandardOutput
();
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, it describes much of what you want
Re: Run executable from within C++/Qt
Dear Santosh,
Thanks again.
The below code makes a QString that is empty as this does not print anything on the screen
Code:
arguments << "-b" << "-t" << "input.txt";
myProcess->start(program, arguments);
if(myProcess->waitForStarted(5000))
{
QByteArray out_data
= myProcess
->readAllStandardOutput
();
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:
Code:
system("./ent -b -t input.txt");
Do you advise against using the latter version?
Re: Run executable from within C++/Qt
Try this
Code:
myProcess
->setReadChannel
(QProcess::StandardOutput);
myProcess->waitForReadyRead();
myProcess->readAllStandardOutput();
Re: Run executable from within C++/Qt
Dear Santosh,
Thanks a lot. This beautifully solved it and everything works well now.
Code:
arguments << "-b" << "-t" << "input.txt";
myProcess->start(program, arguments);
if(myProcess->waitForStarted(5000))
{
myProcess
->setReadChannel
(QProcess::StandardOutput);
myProcess->waitForReadyRead();
QByteArray out_data
= myProcess
->readAllStandardOutput
();
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:
Code:
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?
Re: Run executable from within C++/Qt
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.