PDA

View Full Version : Running Python script on buttonclick in QT creator



dustin007
20th August 2017, 11:35
I am using qtcreator 4.0.1. I want to call python script in cmd prompt on a button click in qt creator. Using QProcess procedure is not resulting in any output. I have already many python scripts which needs to be incorporated into qt, so cannot go with using PythonQT. So I used cmd prompt to run scripts. Here is my code,but i am getting only the cmd prompt with no output.I have looked other ways of calling python scripts but didn't recieved expected results.



void Databasewin::store_clicked()
{
QString cmd_qt = QString("C:/Python27/python p2.py");

const char* cmd1 = cmd_qt.toLocal8Bit().constData();
system(cmd1);
system("pause>nul");
}

d_stranz
20th August 2017, 18:02
You could try using QProcess instead:



QString program( "C:/Python27/python.exe" );
QStringList args = QStringList() << "py2.py";
int exitCode = QProcess::execute( program, args );


If exitCode is -2, it means the program could not be started; if exitCode is -1 it means the program crashed. Otherwise, exitCode will be the exit value from python.exe when it exits.

Also, be sure that your program and python's idea of the working directory is the same as yours. It is entirely possible that python is looking in C:/Python27 for the script, when in fact the script is somewhere else entirely. You could test this simply by replacing "py2.py" with "<absolute-path-to-scripts>/py2.py".