QProcess return value
-
Hello!!
I want to create a program that returns a double[]. And I want to execute this program with a QProcess and get this double[]. Is there any way to do this?
Thank you very much!
@ivanicy
No.A program can only exit with an
int
status.If you need to "pass" anything else back, you could:
-
Save it to file, caller reads file.
-
Write to
stdout
, have caller read that. (That's the same principle as the previous one, just using a stream/pipe instead of a file.) -
Use sockets or shared memory or other IPC.
-
Give up on it being a separate process, and write it as a separate thread instead, sharing a memory area.
Given your use of
QProcess
, I think #2 would be the simplest. -
-
@ivanicy
No.A program can only exit with an
int
status.If you need to "pass" anything else back, you could:
-
Save it to file, caller reads file.
-
Write to
stdout
, have caller read that. (That's the same principle as the previous one, just using a stream/pipe instead of a file.) -
Use sockets or shared memory or other IPC.
-
Give up on it being a separate process, and write it as a separate thread instead, sharing a memory area.
Given your use of
QProcess
, I think #2 would be the simplest.This post is deleted! -
-
#include <iostream> #include <stdlib.h> using namespace std; int main(int argc, char **argv) { double d1 = atof(argv[1]); double d2 = atof(argv[2]); cout << (d1+d2); return 0; }
Qt program
const QString executablepath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); const QString executable = QDir::toNativeSeparators(executablepath + "/a.out"); QStringList arg; arg << "4.1" << "3.2"; QProcess *process = new QProcess(nullptr); connect(process, &QProcess::readyReadStandardOutput, [process]() { QString output = process->readAllStandardOutput(); double d = output.toDouble(); qDebug() << "The sum becomes " << d; }); connect(process, &QProcess::readyReadStandardError, [process]() { QString err = process->readAllStandardError(); qDebug() << "error: " << err; }); process->setWorkingDirectory(executablepath); process->start(executable, arg);
-
#include <iostream> #include <stdlib.h> using namespace std; int main(int argc, char **argv) { double d1 = atof(argv[1]); double d2 = atof(argv[2]); cout << (d1+d2); return 0; }
Qt program
const QString executablepath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); const QString executable = QDir::toNativeSeparators(executablepath + "/a.out"); QStringList arg; arg << "4.1" << "3.2"; QProcess *process = new QProcess(nullptr); connect(process, &QProcess::readyReadStandardOutput, [process]() { QString output = process->readAllStandardOutput(); double d = output.toDouble(); qDebug() << "The sum becomes " << d; }); connect(process, &QProcess::readyReadStandardError, [process]() { QString err = process->readAllStandardError(); qDebug() << "error: " << err; }); process->setWorkingDirectory(executablepath); process->start(executable, arg);
@posktomten said in QProcess return value:
const QString executablepath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath());
const QString executable = QDir::toNativeSeparators(executablepath + "/a.out");Note:
toNativeSeparators
is not needed if you pass that result to Qt functions. It is only needed if you want to display that to the user.Regards
-
Yes! You are absolutely right.
// const QString executablepath = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()); // const QString executable = QDir::toNativeSeparators(executablepath + "/a.out"); const QString executablepath = QCoreApplication::applicationDirPath(); const QString executable = executablepath + "/a.out"; or const QString executablepath = QCoreApplication::applicationDirPath(); const QString executable = executablepath + "/a.out.exe";