Skip to content

QProcess

class QCoroProcess : public 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 or timed out.

See documentation for QProcess::waitForStarted() for details.

Awaitable auto QCoroProcess::waitForStarted(int timeout = 30'000);
Awaitable auto 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 or timed out.

See documentation for [QProcess::waitForFinished()][qtdoc-qprocess-waitForFinished] for details.

Awaitable auto QCoroProcess::waitForFinishedint timeout = 30'000);
Awaitable auto 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.

Awaitable auto QCoroProcess::start(QIODevice::OpenMode openMode) = QIODevice::ReadOnly;
Awaitable auto QCoroProcess::start(const QString &program, const QStringList &arguments,
                                   QIODevice::OpenMode openMode = QIODevice::ReadOnly);

Examples

QCoro::Task<QStringList> listDir(const QString &dirPath) {
    QProcess process;
    qDebug() << "Starting ls...";
    co_await process.start(QStringLiteral("/bin/ls"), {dirPath});
    qDebug() << "Ls started, reading directory...";

    co_await process.waitForFinished();
    qDebug() << "Done";

    return process.readAll();
}