QProcess
Module | Core |
---|---|
Include |
|
CMake |
|
QMake |
|
Inherits | QCoroIODevice |
QProcess
normally has two features to wait for asynchronously: the process to start
and to finish. Since QProcess
itself doesn't provide the ability to co_await
those operations,
QCoro provides a wrapper class QCoroProcess
. To wrap a QProcess
object into the QCoroProcess
wrapper, use qCoro()
:
QCoroProcess qCoro(QProcess &);
QCoroProcess qCoro(QProcess *);
Same as QProcess
is a subclass of QIODevice
, QCoroProcess
subclasses QCoroIODevice
,
so it also provides the awaitable interface for selected QIODevice functions.
See QCoroIODevice
documentation for details.
waitForStarted()
Waits for the process to be started or until it times out. Returns bool
indicating
whether the process has started successfuly (true
) or timed out (false
).
See documentation for QProcess::waitForStarted()
for details.
QCoro::Task<bool> QCoroProcess::waitForStarted(int timeout = 30'000);
QCoro::Task<bool> QCoroProcess::waitForStarted(std::chrono::milliseconds timeout);
waitForFinished()
Waits for the process to finish or until it times out. Returns bool
indicating
whether the process has finished successfuly (true
) or timed out (false
).
See documentation for [QProcess::waitForFinished()
][qtdoc-qprocess-waitForFinished] for details.
QCoro::Task<bool> QCoroProcess::waitForFinishedint timeout = 30'000);
QCoro::Task<bool> QCoroProcess::waitForFinished(std::chrono::milliseconds timeout);
start()
QCoroProcess provides an additional method called start()
which is equivalent to calling
QProcess::start()
followed by QCoroProcess::waitForStarted()
. This operation is co_awaitable
as well.
See the documentation for QProcess::start()
and
QProcess::waitForStarted()
for details.o
Returns true
when the process has successfully started, false
otherwise.
QCoro::Task<bool> QCoroProcess::start(QIODevice::OpenMode openMode = QIODevice::ReadOnly,
std::chrono::milliseconds timeout = std::chrono::seconds(30));
QCoro::Task<bool> QCoroProcess::start(const QString &program, const QStringList &arguments,
QIODevice::OpenMode openMode = QIODevice::ReadOnly,
std::chrono::milliseconds timeout = std::chrono::seconds(30));
Examples
#include <QCoroProcess>
QCoro::Task<QByteArray> listDir(const QString &dirPath) {
QProcess basicProcess;
auto process = qCoro(basicProcess);
qDebug() << "Starting ls...";
co_await process.start(QStringLiteral("/bin/ls"), {dirPath});
qDebug() << "Ls started, reading directory...";
co_await process.waitForFinished();
qDebug() << "Done";
return basicProcess.readAll();
}