ฉันต้องการใช้คุณสมบัติบน UI ที่สร้างใน QT ฉันต้องการสิ่งนั้นเมื่อฉันกดปุ่มจาก UI แอปพลิเคชันควรส่งคำสั่งระบบไปยังเครื่อง ตัวอย่างเช่น เมื่อฉันคลิกปุ่ม ควรดำเนินการคำสั่ง sudo systemctl start application.service
ส่วนของไฟล์ QML มีลักษณะดังนี้:
...
UIPage {
id: root
PopupInterface {
id: popupInterface
actions: [ { text: "OK" }, { text: "Cancel" } ]
title: "Settings"
...
onActionTriggered: {
if (actionId === "0" && !currentState) {
Process.start("/bin/cat", [ "/proc/uptime" ]);
} else if (actionId === "0" && currentState) {
Process.start("/bin/cat", [ "/proc/uptime" ]);
}
...
}
...
และนี่คือส่วนหัว:
#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();
}
};
ฉันพบว่ามีโค้ด c++ สำหรับคำสั่งระบบ (http://www.xargs.com/qml/process.html) แต่ฉันไม่รู้ว่าจะวางไว้ที่ไหน (ไฟล์ส่วนหัว) เนื่องจากโครงสร้างโครงการ UI มีความซับซ้อน ฉันกำลังพยายามเพิ่มคุณสมบัตินี้ให้กับ Neptune UI (https://github.com/qtproject/qt-apps-neptune-ui/tree/5.10) และไฟล์ qml สามารถพบได้ใน sysui/display/FunctionsPage.qml
ฉันควรทำอย่างไรเพื่อดำเนินการคำสั่งระบบจากไฟล์ qml