getCommandResult():

reset result if necessary (for instance for showing current result
	in GUI.
This commit is contained in:
Gerhard Hoffmann 2024-12-06 11:48:21 +01:00
parent 11524b8389
commit 0ebf8b095a
2 changed files with 18 additions and 5 deletions

View File

@ -5,6 +5,7 @@
#include <QDir>
#include <QRegularExpression>
#include <QDateTime>
#include <QMutexLocker>
Command::Command(QString const &command, int start_timeout, int finish_timeout)
: m_command(command.trimmed())
@ -14,14 +15,23 @@ Command::Command(QString const &command, int start_timeout, int finish_timeout)
, m_exitCode(-1) {
}
QString Command::getCommandResult() const {
QString Command::getCommandResult(bool reset) const {
QMutexLocker locker(&m_mtx);
if (reset == false) {
return m_commandResult;
}
QString commandResult = m_commandResult;
m_commandResult.clear();
return commandResult;
}
void Command::readyReadStandardOutput() {
QMutexLocker locker(&m_mtx);
QProcess *p = (QProcess *)sender();
m_commandResult += p->readAllStandardOutput();
// qCritical() << m_commandResult;
}
void Command::readyReadStandardError() {
@ -35,6 +45,7 @@ void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
// read all remaining data sent to the process, just in case
QString d = p->readAllStandardOutput();
if (!d.isEmpty()) {
QMutexLocker locker(&m_mtx);
m_commandResult += d;
}
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));

View File

@ -7,22 +7,24 @@
#include <QString>
#include <QStringList>
#include <QProcess>
#include <QMutex>
class Command : public QObject {
Q_OBJECT
QString m_command;
QString m_commandResult;
mutable QString m_commandResult;
int m_waitForStartTimeout;
int m_waitForFinishTimeout;
int m_exitCode;
mutable QMutex m_mtx;
public:
explicit Command(QString const &command,
int start_timeout = 100000,
int finish_timeout = 100000);
QString getCommandResult() const;
QString getCommandResult(bool reset = false) const;
QString command() const { return m_command; }
bool execute(QString workingDirectory, QStringList args = QStringList());