you can write a process executor class like this :
#include <QProcess>
#include <QVariant>
class Process : public QProcess {
Q_OBJECT
public:
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();
}
};
and register them :
#include <QtQml>
#include "process.h"
qmlRegisterType<Process>("Process", 1, 0, "Process");
and finally run your command from 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("poweroff", [ "-f" ]);
}
-f make forced to power off immediately.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…