-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpythonpipe.cpp
66 lines (61 loc) · 1.95 KB
/
pythonpipe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "pythonpipe.h"
#include <qobject.h>
#include <iostream>
PythonPipe::PythonPipe() : QObject() {
connect(&qprocess,
(void(QProcess::*)(int,QProcess::ExitStatus)) & QProcess::finished,
[this](int,QProcess::ExitStatus) mutable {isRunning = false;});
connect(&qprocess, &QProcess::readyReadStandardOutput, [this]() {
QString tmp(qprocess.readAllStandardOutput());
tmp = "{"+pythonScriptName.fileName()+"} " + tmp;
std::cout << tmp.toStdString();
pythonMessage(tmp);
});
connect(&qprocess, &QProcess::readyReadStandardError, [this]() {
QString tmp(qprocess.readAllStandardError());
tmp = "["+pythonScriptName.fileName()+ "] " + tmp;
std::cout << tmp.toStdString();
pythonMessage(tmp);
});
connect(&qprocess, &QProcess::errorOccurred, [this](QProcess::ProcessError e) {
QString tmp;
switch (e) {
case QProcess::FailedToStart:
tmp = "The process failed to start.";
break;
case QProcess::Crashed:
tmp = "The process crashed after starting successfully.";
break;
case QProcess::Timedout:
tmp = "The last waitFor...() function timed out. ";
break;
case QProcess::WriteError:
tmp = "An error occurred when attempting to write to the process.";
break;
default:
tmp = "An unknown error occurred.";
}
tmp = "[" + pythonScriptName.fileName() + "] Error: " + tmp + "\n";
std::cout << tmp.toStdString();
pythonMessage(tmp);
});
}
int PythonPipe::start(QString filename) {
pythonScriptName.setFile(filename);
isRunning = true;
QStringList arguments;
arguments << filename;
qprocess.start("python",arguments);
return 0;
}
void PythonPipe::stop() {
qprocess.close();
isRunning = false;
}
void PythonPipe::write(const char* data)
{
if (!isRunning) return;
if (qprocess.state() == QProcess::Running) {
qprocess.write(data);
}
}