Skip to content

Instantly share code, notes, and snippets.

@iamazeem
Created December 16, 2016 07:10
Show Gist options
  • Save iamazeem/e5279dc1b383ca8c91ea890a9f1c472f to your computer and use it in GitHub Desktop.
Save iamazeem/e5279dc1b383ca8c91ea890a9f1c472f to your computer and use it in GitHub Desktop.
Qt QProcess - Example using Lambda - Run Notepad on Windows
// ----------------------------------------------
// Open Notepad on Windows using QProcess
// Asynchronously using std::async and
// Wait for it to exit with std::future
// ----------------------------------------------
#include <QProcess> // QProcess
#include <QDebug> // qDebug(), qCritical(), ...
#include <future>
int runNotepad()
{
const QString notepadExe { "C:/Windows/notepad.exe" };
QProcess process;
process.start( notepadExe );
process.waitForStarted( -1 );
if ( process.exitCode() != 0 )
{
qCritical() << "Exit Code: " << process.exitCode();
return -1;
}
qDebug() << "Started!";
// Wait for the process to exit asynchronously
auto f = std::async( [&process]()
{
process.waitForFinished( -1 );
qDebug() << "Finished!";
});
// ...
// Do some synchronous work here
// ...
f.get(); // Wait for std::async to return
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment