52 lines
1.9 KiB
C++
52 lines
1.9 KiB
C++
#include "worker.h"
|
|
#include "QDebug"
|
|
|
|
Worker::Worker(const QString &program, const QStringList &arguments, QObject *parent)
|
|
: QObject(parent), process(new QProcess(this)), program(program), arguments(arguments)
|
|
{
|
|
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
this, &Worker::onProcessFinished);
|
|
connect(process, &QProcess::errorOccurred, this, [this](QProcess::ProcessError error){
|
|
qDebug() << "Process error:" << error << ", description:" << process->errorString();
|
|
});
|
|
connect(process, &QProcess::stateChanged, [](QProcess::ProcessState state){
|
|
qDebug() << "Process state changed:" << state;
|
|
});
|
|
|
|
#if _DEBUG
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); // получить текущие системные переменные
|
|
QString path = env.value("PATH");
|
|
path = "D:\\Qt6-8\\Tools\\CMake_64\\bin;" + path; // добавить нужный путь в начало PATH
|
|
env.insert("PATH", path);
|
|
process->setProcessEnvironment(env);
|
|
#endif
|
|
}
|
|
|
|
void Worker::start() {
|
|
qDebug() << "Starting process:" << program << arguments.join(" ");
|
|
process->start(program, arguments);
|
|
|
|
// process->startDetached("program", arguments)
|
|
// if (!process->startDetached("program", arguments)) {
|
|
// qDebug() << "Failed to start process";
|
|
// }
|
|
qDebug() << process->state();
|
|
}
|
|
|
|
void Worker::stop() {
|
|
if (process->state() != QProcess::NotRunning) {
|
|
qDebug() << "Stopping process...";
|
|
process->kill();
|
|
process->waitForFinished();
|
|
}
|
|
}
|
|
|
|
void Worker::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
|
|
qDebug() << "Process finished with:"
|
|
<< "\n Exit code:" << exitCode
|
|
<< "\n Status:" << (exitStatus == QProcess::NormalExit ? "Normal" : "Crash");
|
|
|
|
// emit finished(exitCode, exitStatus);
|
|
emit finished();
|
|
}
|