Question

I'm trying to execute a process in Qt (in Linux) that executes a python script and I haven't been able to make it work. This is the code:

QProcess process;
QString scriptFile = "../../scriptPath/script.py";

QString pyCommand = "\"python " + scriptFile + 
                    " -f " + parameter1 +
                    " -t parameter2" +
                    " -v parameter3" +
                    " -e " + parameter4 + "\"";

printf("PyCommand: %s\n", qStringToStdString(pyCommand).c_str());

process.start ("bash", QStringList () << "-c" << pyCommand);

Where parameter1 and parameter3 are QStrings that I get from file dialogs (both paths are correct) and parameter2 and parameter4 are hardcoded strings (just in case, these strings have characters "-" and "." in them). Function qStringToStdString is a function I have created to transform a QString to a std::string and it works just fine (I already have used it).

PyCommand is printed in the console and looks fine. In fact, if I open a terminal and try to execute bash -c followed by what I get in the printf instruction it works. But it doesn't work when I execute the Qt application.

Can someone help me out here? Something I'm missing?

Was it helpful?

Solution

Your code is ignoring how the API was designed to work. You should use either of the alternatives presented below. Using the python interpreter explicitly depends on whether you use the shebang properly in your python script. I have just let it in for reference, but you can easily get rid of that. It is not the main point of your issue in here.

First alternative (QStringList based)

QProcess process;
QString scriptFile =  QCoreApplication::applicationDirPath() + "../../scriptPath/script.py";

QStringList pythonCommandArguments = QStringList() << scriptFile
    << "-f " << parameter1 << "-t" <<  parameter2 << "-v"
    <<  parameter3 << "-e" << parameter4;

printf("PyCommand: %s\n", qStringToStdString(pythonCommandArguments.join(' ')).c_str());

process.start ("python", pythonCommandArguments);

Second alternative (QString based)

QProcess process;
QString scriptFile =  QCoreApplication::applicationDirPath() + "../../scriptPath/script.py";

QString pythonCommand = "python " + scriptFile + 
                    " -f " + parameter1 +
                    " -t parameter2" +
                    " -v parameter3" +
                    " -e " + parameter4;

printf("PyCommand: %s\n", qStringToStdString(pythonCommand).c_str());

process.start (pythonCommand);

Here you can find the proper method signatures for both ways:

  • void QProcess::start(const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite)

http://doc.qt.io/qt-5/qprocess.html#start

  • void QProcess::start(const QString & command, OpenMode mode = ReadWrite)

http://doc.qt.io/qt-5/qprocess.html#start-3

OTHER TIPS

You need to use QStringList() for command line parameter.

Your pyCommand must be QStringList.

Something like this:

QStringList pyCommand;
pyCommand << "-f" << parameter1 <<
                "-t" << parameter2 <<
                "-v" << parameter3 <<
                "-e" << parameter4;

process.start(scriptFile, pyCommand);

See the docs: http://doc.qt.io/qt-5/qprocess.html#start

So at the end I got it working. As Laszlo commented out, the way it should be executed is starting a process with python and pass all the arguments as a QStringList. That's what I tried at the beginning and it wasn't working at all. So I looked for information on the internet and I saw that many people were executing a bash process and then they were passing a string with the python command instead of doing it as I was. I tried to do it that way but it wasn't working either...

The solution? Well, I've done what I should have done before asking the question here. I connected the most important signals of the process (started, finished and error) with some simple methods that wrote output. At the end I found out that python was returning 2 (No such file or directory) as the exitCode.

It seems that the application wasn't taking the executable path as the root path and therefore the path I was referring to ("../../scriptPath/script.py") didn't exist. In order to get the proper binary path I use now QDir::currentPath() + "/scriptPath/script.py".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top