现在,我所拥有的只有我的QML文件和按钮。
/*PlasmaComponents.*/ToolButton {
id: shutdownButton
text: i18n("Shutdown")
iconSource: "system-shutdown"
enabled: power.canShutdown
onClicked: doTheThing();
}从阅读有关QML的内容来看,我似乎需要添加一个C++进程。使用QML4可以做到这一点吗?如果不能,QProcess能工作吗?如果是这样,需要更改哪些文件?
发布于 2021-01-23 13:14:17
您可以像这样编写流程执行器类:
#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被迫立即关闭电源。
https://stackoverflow.com/questions/65853041
复制相似问题