0

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;
3
  • How you know it fails? whats the error or unexpected behavior? Commented May 15, 2018 at 17:13
  • 2
    QStringList arguments({"test.py" ,"arg1" ,"arg2", "arg3"}); Commented May 15, 2018 at 18:02
  • @MohammadKanan The parentheses are unnecessary: QStringList arguments{"test.py", "arg1", "arg2", "arg3"}; Commented May 16, 2018 at 2:29

1 Answer 1

1
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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.