Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess return value
Forum Updated to NodeBB v4.3 + New Features

QProcess return value

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • ivanicyI ivanicy

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ivanicy
      No.

      A program can only exit with an int status.

      If you need to "pass" anything else back, you could:

      1. Save it to file, caller reads file.

      2. 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.)

      3. Use sockets or shared memory or other IPC.

      4. 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.

      posktomtenP 1 Reply Last reply
      7
      • JonBJ JonB

        @ivanicy
        No.

        A program can only exit with an int status.

        If you need to "pass" anything else back, you could:

        1. Save it to file, caller reads file.

        2. 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.)

        3. Use sockets or shared memory or other IPC.

        4. 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.

        posktomtenP Offline
        posktomtenP Offline
        posktomten
        wrote on last edited by posktomten
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • posktomtenP Offline
          posktomtenP Offline
          posktomten
          wrote on last edited by Chris Kawa
          #4
          #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);
          
          aha_1980A 1 Reply Last reply
          6
          • posktomtenP posktomten
            #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);
            
            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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

            Qt has to stay free or it will die.

            1 Reply Last reply
            4
            • posktomtenP Offline
              posktomtenP Offline
              posktomten
              wrote on last edited by posktomten
              #6

              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";
              
              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved