-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessHelper.cpp
66 lines (60 loc) · 1.67 KB
/
ProcessHelper.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 <QFile>
#include <QTextStream>
#include "ProcessHelper.h"
#include "WidgetContainer.h"
ProcessHelper::ProcessHelper() : active(false)
{
}
void ProcessHelper::Start(QString qcommand, WidgetContainer* w, QString outfile)
{
command = qcommand;
widget = w;
ofile = outfile;
process = QProcessPtr(new QProcess());
QObject::connect(&(*process), SIGNAL(readyReadStandardOutput()), this, SLOT(ReadOutput()));
QObject::connect(&(*process), SIGNAL(readyReadStandardError()), this, SLOT(ReadError()));
QObject::connect(&(*process), SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ProcessFinished(int, QProcess::ExitStatus)));
active = true;
process->start("sh", QStringList() << "-c" << qcommand);
}
bool ProcessHelper::Active()
{
return active;
}
void ProcessHelper::ReadOutput()
{
QByteArray stdout = process->readAllStandardOutput();
QString newtext(stdout);
text += newtext;
}
void ProcessHelper::ReadError()
{
QByteArray stderr = process->readAllStandardError();
QString newtext(stderr);
error += newtext;
}
void ProcessHelper::ProcessFinished(int retcode, QProcess::ExitStatus status)
{
if (retcode == 0 && status == 0)
{
// Write output to the file specified by the caller
if (ofile != "")
{
QFile f(ofile);
if (f.open(QIODevice::WriteOnly))
{
QTextStream output(&f);
output << text << endl;
}
}
widget->ProcessFinished(text);
}
else
{
cout << "Command '" << command.toStdString() << "' failed with return code: " << retcode << endl;
cout << error.toStdString() << endl;
}
text = "";
error = "";
active = false;
}