Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitting signals for standard output and error output #375

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions systemcommandexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@ class SystemCommandExecutor : public QObject

process.setReadChannel(QProcess::StandardOutput);

auto conn = connect(&process, SIGNAL(finished()), &loop, SLOT(quit()));
QTimer::singleShot(0, [&, command]() {
process.start("sh", QStringList() << "-c" << command);
auto conn1 = connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
auto conn2 = connect(&process, &QProcess::errorOccurred, &loop, &QEventLoop::quit);
auto conn3 = connect(&process, &QProcess::readyReadStandardOutput, [&]() {
emit processOutput(QString::fromUtf8(process.readAllStandardOutput()));
});

auto conn2 = connect(&process, &QIODevice::readyRead, [&]() {
emit processOutput(QString::fromUtf8(process.readAll()));
auto conn4 = connect(&process, &QProcess::readyReadStandardError, [&]() {
emit processOutput(QString::fromUtf8(process.readAllStandardError()));
});

process.start("sh", QStringList() << "-c" << command);

if (!process.waitForStarted()) {
disconnect(conn1);
disconnect(conn2);
disconnect(conn3);
disconnect(conn4);
return "Failed to start process";
}

loop.exec();
disconnect(conn);

disconnect(conn1);
disconnect(conn2);
disconnect(conn3);
disconnect(conn4);

QString output = process.readAllStandardOutput();
QString errorOutput = process.readAllStandardError();
Expand Down
Loading