Cleanup python script running
This commit is contained in:
parent
87db38b08e
commit
1eac3f6a83
7 changed files with 68 additions and 128 deletions
|
|
@ -1,26 +1,54 @@
|
|||
|
||||
#include "pythonrunner.h"
|
||||
#include "pythonembed.h"
|
||||
#include <QDebug>
|
||||
|
||||
PythonRunner::PythonRunner(QTextEdit* outputWidget, QObject* parent)
|
||||
: QObject(parent), m_outputWidget(outputWidget), m_pythonEmbed(nullptr) {
|
||||
m_pythonEmbed = new PythonEmbed(m_outputWidget, this);
|
||||
: QObject(parent), m_outputWidget(outputWidget), m_process(nullptr) {
|
||||
m_process = new QProcess(this);
|
||||
connect(m_process, &QProcess::readyReadStandardOutput, this, &PythonRunner::onOutputAvailable);
|
||||
connect(m_process, &QProcess::readyReadStandardError, this, &PythonRunner::onErrorAvailable);
|
||||
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &PythonRunner::onProcessFinished);
|
||||
}
|
||||
|
||||
PythonRunner::~PythonRunner() {
|
||||
delete m_pythonEmbed;
|
||||
if (m_process) {
|
||||
m_process->terminate();
|
||||
m_process->waitForFinished(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void PythonRunner::runScript(const QString& scriptContent) {
|
||||
if (m_pythonEmbed) {
|
||||
m_pythonEmbed->runScript(scriptContent);
|
||||
if (m_process->state() == QProcess::Running) {
|
||||
m_process->terminate();
|
||||
m_process->waitForFinished(1000);
|
||||
}
|
||||
|
||||
m_outputWidget->clear();
|
||||
m_process->start("python3", QStringList() << "-u" << "-c" << scriptContent);
|
||||
}
|
||||
|
||||
void PythonRunner::stopScript() {
|
||||
if (m_pythonEmbed) {
|
||||
m_pythonEmbed->stopScript();
|
||||
if (m_process && m_process->state() == QProcess::Running) {
|
||||
m_process->terminate();
|
||||
}
|
||||
}
|
||||
|
||||
#include "pythonrunner.moc"
|
||||
void PythonRunner::onOutputAvailable() {
|
||||
QByteArray output = m_process->readAllStandardOutput();
|
||||
m_outputWidget->append(output);
|
||||
m_outputWidget->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
||||
void PythonRunner::onErrorAvailable() {
|
||||
QByteArray error = m_process->readAllStandardError();
|
||||
m_outputWidget->append(error);
|
||||
m_outputWidget->moveCursor(QTextCursor::End);
|
||||
}
|
||||
|
||||
void PythonRunner::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
if (exitStatus == QProcess::NormalExit && exitCode != 0) {
|
||||
m_outputWidget->append(QString("Process exited with code %1\n").arg(exitCode));
|
||||
} else if (exitStatus == QProcess::CrashExit) {
|
||||
m_outputWidget->append("Python was stopped\n");
|
||||
}
|
||||
emit scriptFinished(exitCode);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue