问题
I want to run a python script in my qt project after clicking a pushButton using QProcess. This is my code :
void MainWindow::on_pushButton_clicked()
{
QProcess p;
QStringList params;
QString pythonPath = "C:/Python/python.exe";
QString pythonScript = "C:\\Users\\melek\\Desktop\\user_interface\\user_interface-master\\match.py";
params << pythonScript;
QStringList arguments;
arguments <<"-t1"<<"start.png"<<"-t2"<< "end.png"<<"-i"<< "images_";
p.setArguments(arguments);
p.start(pythonPath, params);
p.execute(pythonPath,params);
p.waitForFinished(-1);
QString p_stdout = p.readAll();
QString output(p.readAllStandardOutput());
QString p_stderr = p.readAllStandardError();
if(!p_stderr.isEmpty())
qDebug()<<"Python error:"<<p_stderr;
qDebug()<<"Python result="<<p_stdout;
qDebug()<<p_stdout;
qDebug()<<output;
}
The python arguments are shown in the python code :
ap = argparse.ArgumentParser()
ap.add_argument("-t1", "--template1", required=True, help="Path to template image")
ap.add_argument("-t2", "--template2", required=True, help="Path to template image")
ap.add_argument("-i", "--images", required=True,
help="Path to images where template will be matched")
ap.add_argument("-v", "--visualize",
help="Flag indicating whether or not to visualize each iteration")
This is what I write in the terminal of python to run the program correctly:
path_to_script\python python match.py --template1 start.png --template2 end.png --images images_
Where templates 1 and 2 are two png pictures and images_
is a folder and --visualize
must'n t be given in the terminal.
Once I run the program in QT, I receieve the following error:
usage: match.py [-h] -t1 TEMPLATE1 -t2 TEMPLATE2 -i IMAGES [-v VISUALIZE]
match.py: error: the following arguments are required: -t1/--template1, -t2/--template2, -i/--images
来源:https://stackoverflow.com/questions/61567866/set-arguments-with-diffrent-formats-in-qprocess