Created
December 16, 2016 07:10
-
-
Save iamazeem/e5279dc1b383ca8c91ea890a9f1c472f to your computer and use it in GitHub Desktop.
Qt QProcess - Example using Lambda - Run Notepad on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------- | |
// 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