现在,我所有的是我的QML文件,与按钮。
/*PlasmaComponents.*/ToolButton {
id: shutdownButton
text: i18n("Shutdown")
iconSource: "system-shutdown"
enabled: power.canShutdown
onClicked: doTheThing();
}
从阅读关于QML,似乎我需要添加一个c++进程。QML4是否可以实现这一点?如果不能,QProcess可以工作吗?如果是这样,哪些文件需要更改?
您可以像这样编写一个流程执行器类:
#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();
}
};
并注册它们:
#include <QtQml>
#include "process.h"
qmlRegisterType<Process>("Process", 1, 0, "Process");
,最后从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强制立即关机。