Reputation: 146
I cannot figure out why this does not work:
void Controller::on_buttonVisualTracking_clicked()
{
QProcess *trackingProcess = new QProcess();
trackingProcess->start("python C:\\visualTracking.py");
}
The specific script here is a pychart script, and if I simply run it from the command line, it executes correctly, opening up a window that displays the chart. That is something that I should emphasize, I'm expecting a new window to open displaying the pychart, which is what I get if I run the script myself.
I also tried this code to see if QProcess simply wasn't working. However this worked as expected and an empty notedpad window appeared.
void Controller::on_buttonVisualTracking_clicked()
{
QProcess *trackingProcess = new QProcess();
trackingProcess->start("notepad");
}
So then I thought maybe something is wrong with how I'm supplying arguments, so I tried this, which opened a notepad window with the visualTracking.py text as you would expect.
void Controller::on_buttonVisualTracking_clicked()
{
QProcess *trackingProcess = new QProcess();
trackingProcess->start("notepad C:\\visualTracking.py");
}
Thus, I'm completely at a loss. Why will QProcess not open up the python script?
UPDATE: Per suggestions I've now tried these two options, neither one worked.
void Controller::on_buttonVisualTracking_clicked()
{
QString run = "C:\\Development\\Anaconda3\\python.exe";
QStringList args;
args << "C:\\visualTracking.py";
QProcess *trackingProcess = new QProcess();
trackingProcess->start(run, args);
}
and
void Controller::on_buttonVisualTracking_clicked()
{
QString commands = "python C:\\visualTracking.py";
QProcess *trackingProcess = new QProcess();
trackingProcess->start("cmd");
trackingProcess->write(commands.toLatin1().data());
if(!trackingProcess->waitForStarted()){};
}
UPDATE: I thought I had a solution to this, but unfortunately I'm once again, on the development machine, unable to run python scripts again. I've no idea why the behavior changes randomly. My only guess is some windows security setting blocking my app from running a script, but I do not have any evidence to suggest this
Upvotes: 2
Views: 2311
Reputation: 98465
I've had a similar problem with an interactive script; the solution was to force it to run interactively:
auto *process = new QProcess{this};
connect(process, &QProcess::errorOccurred, []{
qFatal("process error occurred");
});
process->start("python", {"-i", "myscript.py"});
Upvotes: 0
Reputation: 25
Are you using Qt5.8.0 MinGW version? I encountered the same issue and after switching to the Qt5.8.0 MSVC version everything worked just fine.
I haven't tried other version yet, but I think this may be the problem. Hope this help
UPDATE
I tried the QT 5.10.0 MinGW version, the bug still exist. However, using the gcc and g++ of manual installed MinGW won't have the same problem. I guess it's because the MinGW version shipped with the Qt is probably too old?
Upvotes: 0
Reputation: 12781
I see this problem.
But able to resolve by starting cmd
first.
Also call ::waitForStarted
to block until the process has started.
Check if it works for you (Details in comments).
//YOUR PY COMMAND
QString pyCommand("python C:\\visualTracking.py \n"); //try with out " \n" also...
//CREATE A PROCESS OBJECT
QProcess *qprocess = new QProcess(this);
//START THE CMD
qprocess->start("cmd");
//WRITE YOUR PY COMMAND TO PROCESS
qprocess->write(pyCommand.toLatin1().data());
//BLOCK THE PROCESS UNTILL IT STARTED
if (!qprocess->waitForStarted())
{
}
Upvotes: 0
Reputation: 5466
With QProcess
, you can only start real executables, not scripts. Therefore, you need to run the python interpreter python.exe
and give your script as an argument.
See this answer for an example on how to do that. You might need to specify the full path to python.exe
to get it to work for you, for example "C:\\Python26\\python.exe"
.
Upvotes: 0