1

I have a Python script that takes three folders as input. I am trying to create a GUI in which I browse for the three folder locations, then call the Python script with those as parameters. I've already create the GUI and can browse for the folder locations, but I seem to have problems calling the Python script using either QProcess or Python.h.

QProcess:

QString arg1 = ui->folder1->text();
QString arg2 = ui->folder2->text();
QString arg3 = ui->folder3->text();

QProcess p;
QString script = "python script.py";
QStringList params;

params << arg1 << arg2 << arg3;
p.start(script, params);
p.waitForFinished(-1);

QString p_stdout = p.readAll();
ui->displayOutput->setText(p_stdout);

Python.h:

QString arg1 = ui->folder1->text();
QString arg2 = ui->folder2->text();
QString arg3 = ui->folder3->text();

const char* args1 = arg1.toUtf8().constData();
const char* args2 = arg2.toUtf8().constData();
const char* args3 = arg3.toUtf8().constData();

PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pResult, *pArgs;

// Initialize Python Interpreter
Py_Initialize();

// Convert python script's name into Python string
pName = PyString_FromString("script");

// Import the file as a Python module
pModule = PyImport_Import(pName);

// Create a dictionary for the contents of the module
pDict = PyModule_GetDict(pModule);

// Arguments
pArgs = PyTuple_New(3);
PyTuple_SetItem(pArgs, 0, PyString_FromString(args1));
PyTuple_SetItem(pArgs, 1, PyString_FromString(args2));
PyTuple_SetItem(pArgs, 2, PyString_FromString(args3));

pFunc = PyDict_GetItemString(pDict, "main");

pResult = PyObject_CallObject(pFunc, pArgs);

Py_Finalize();

I will say that when I do run the Python.h in either debug or Release, the program crashes when I press the button to run the code. Is there a better way to run either code without running into issues?

Edit: It is able to run without arguments using the QProcess method, yet will not run when I include the arguments, which are folder paths. I made a function to put quotations to those folders that have whitespace in them, but it still won't run. Is there a way to pass in the folders as parameters so that the Python script can run?

6
  • You should specify the path where the script lies. QProceas gives such a possibility. Also an empty space in the scripts name requires maybe a backslash. Commented Jan 31, 2017 at 15:02
  • When I use the QProcess method, nothing happens, as if the button has no code. I don't know if I'm missing anything else. Commented Jan 31, 2017 at 15:23
  • I don't think it finds the path to your script. See my first comment. Have you tried QProcess::SetWorkingDirectory ? Commented Jan 31, 2017 at 15:40
  • That works, but now I believe the issue is that the folders that have spaces in them is causing it to not run. Commented Jan 31, 2017 at 16:18
  • Why not just use QTPython? Commented Feb 2, 2017 at 14:57

2 Answers 2

1

So after trying different methods, this is what works for me on Windows

QString cmd_qt = QString("python %1script.py %2 %3 %4").arg(filename1).arg(filename3).arg(filename4).arg(filename5);
const char* cmd = cmd_qt.toLocal8Bit().constData();
system(cmd);

This just calls the Python function via command prompt. I don't know if this is the most efficient way. but it works for now. If there is a better way, let me know.

Sign up to request clarification or add additional context in comments.

Comments

0
QString script = "python "+ "../folder1/script.py;
const char* cmd = script.toLocal8Bit().constData();
system(cmd);

is not reading the content correctly.

The output shows Invalid Test_log directory always.

But when running the python script.py from command line directly gives proper o/p. Thepython path is set in my envionment variable .

 log_dir = '../Test_Logs/'
if not os.path.exists(log_dir):
    print('Invalid Test_log directory')
    exit()

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.