Running an external program, system command, or shell script from QML

Only a few lines of C++ are needed to expose the Qt QProcess class to QML so that external commands/processes can be run directly from QML:

#include <QProcess>
#include <QVariant>

class Process : public QProcess {

    Process(QObject *parent = 0) : QProcess(parent) { }

    Q_INVOKABLE void start(const QString &program, const QVariantList &arguments) {
        QStringList args;

        // convert QVariantList from QML to QStringList for QProcess 

        for (int i = 0; i < arguments.length(); i++)
            args << arguments[i].toString();

        QProcess::start(program, args);

    Q_INVOKABLE QByteArray readAll() {
        return QProcess::readAll();
This class exposes the start and readAll methods to QML; other methods can be exposed similarly if needed. All of QProcess' signals are automatically exposed to QML. To register the Process class with QML, add the following to main():
#include <QtQml>
#include process.h

qmlRegisterType<Process>(Process, 1, 0, Process);
An example of using the Process element in QML:
import QtQuick 2.4
import QtQuick.Controls 1.3
import Process 1.0

ApplicationWindow {
    width: 800
    height: 480
    visible: true

    Text {
        id: text

    Process {
        id: process
        onReadyRead: text.text = readAll();

    Timer {
        interval: 1000
        repeat: true
        triggeredOnStart: true
        running: true
        onTriggered: process.start(/bin/cat, [ /proc/uptime ]);

Copyright © 2015 John Temples (qml at xargs dot com)