Skip to content

Instantly share code, notes, and snippets.

@warunanc
Forked from ynonp/main.cpp
Created November 21, 2017 06:29
Show Gist options
  • Save warunanc/265fcb8ab5c5bb65ad9349a7c21cb782 to your computer and use it in GitHub Desktop.
Save warunanc/265fcb8ab5c5bb65ad9349a7c21cb782 to your computer and use it in GitHub Desktop.
QProcess output example
#include <QCoreApplication>
#include <QtCore/QtCore>
#include "monitor.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess p;
p.start("/bin/ls");
qDebug() << "Let's Go";
Monitor m;
QObject::connect(&p, SIGNAL(finished(int,QProcess::ExitStatus)), &m, SLOT(finished(int,QProcess::ExitStatus)));
QObject::connect(&p, SIGNAL(readyReadStandardOutput()), &m, SLOT(readyReadStandardOutput()));
QObject::connect(&p, SIGNAL(started()), &m, SLOT(started()));
return a.exec();
}
#include "monitor.h"
#include <QtCore/QtCore>
#include <QProcess>
Monitor::Monitor(QObject *parent) :
QObject(parent)
{
}
void Monitor::error(QProcess::ProcessError error)
{
qDebug() << "Error: " << error;
}
void Monitor::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "Finished: " << exitCode;
qApp->exit();
}
void Monitor::readyReadStandardError()
{
qDebug() << "ReadyError";
}
void Monitor::readyReadStandardOutput()
{
qDebug() << "readyOut";
QProcess *p = (QProcess *)sender();
QByteArray buf = p->readAllStandardOutput();
qDebug() << buf;
}
void Monitor::started()
{
qDebug() << "Proc Started";
}
#ifndef MONITOR_H
#define MONITOR_H
#include <QObject>
#include <QtCore/QtCore>
class Monitor : public QObject
{
Q_OBJECT
public:
explicit Monitor(QObject *parent = 0);
signals:
public slots:
void error(QProcess::ProcessError error);
void finished(int exitCode, QProcess::ExitStatus exitStatus);
void readyReadStandardError();
void readyReadStandardOutput();
void started();
};
#endif // MONITOR_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment