cnm
cnm

Reputation: 113

how to handle input parameters in QProcess

The following simple python code requires three input arguments for 'test.py' (besides 'python' and 'test.py') in command line:

#!/usr/bin/python
import sys

def main(argv):
   if (len(sys.argv) < 4):
       print ('argv must be greater than 4')
   else:
     print ('Number of arguments:', len(sys.argv), 'arguments.')
     print ('Argument List:', str(sys.argv))

if __name__ == "__main__":
   main(sys.argv[1:])

Run test.py:

C:\>python test.py arg1 arg2
argv must be greater than 4

C:\>python tt.py arg1 arg2 arg3
Number of arguments: 4 arguments.
Argument List: ['tt.py', 'arg1', 'arg2', 'arg3']

I use the following simple Qt code, but it fails to produce the above result. Is there a way in Qt to mimic the above command line, i.e. 'python command arg1, ... argN'. NOTE: 'python' must be used in this case.

   QProcess *qtq = new QProcess();
   QString program("python");
   QStringList arguments("test.py arg1 arg2 arg3");
   qtq->setProgram(program);
   qtq->setArguments(arguments);
   qtq->start();
   qtq->waitForReadyRead();
   qtq->waitForFinished();
   QByteArray s =  qtq->readAll();
   qDebug() << s;

Upvotes: 0

Views: 970

Answers (1)

docsteer
docsteer

Reputation: 2516

QStringList arguments("test.py arg1 arg2 arg3");

That is your problem line. That will create a QStringList with only one string in it, which gets passed as one argument to python. You should instead do:

QStringList arguments;
arguments << QString("test.py");   
arguments << QString("arg1");
arguments << QString("arg2");
arguments << QString("arg3");

Upvotes: 1

Related Questions