Skip to content

Instantly share code, notes, and snippets.

@mitchcurtis
Created May 30, 2024 07:03
Show Gist options
  • Save mitchcurtis/1388f4ac1198de486ff0f8af359857a7 to your computer and use it in GitHub Desktop.
Save mitchcurtis/1388f4ac1198de486ff0f8af359857a7 to your computer and use it in GitHub Desktop.
Execute shell script using QProcess
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QProcess process;
QObject::connect(&process, &QProcess::errorOccurred, [&process](){
qWarning() << "Error starting shell:" << process.errorString();
QCoreApplication::quit();
});
QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process](){
qDebug() << process.readAllStandardOutput();
});
QObject::connect(&process, &QProcess::readyReadStandardError, [&process](){
qWarning() << "Error running script:" << process.readAllStandardError();
});
QObject::connect(&process, &QProcess::finished, [&process](){
QCoreApplication::quit();
});
process.start("/usr/bin/sh", { "-c", "echo hello" });
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment