Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QProcess not executing a python script

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?

like image 715
Carlos Pastor Avatar asked Dec 04 '22 09:12

Carlos Pastor


1 Answers

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

like image 138
lpapp Avatar answered Dec 09 '22 15:12

lpapp