我不是开发人员,我才刚刚开始(20 天),所以我不明白一切,而且我可能会犯很多错误
我正在尝试为 KDE Plasma 6 创建一个 plasmoid,它具有通过动态生成的切换开关启动脚本/应用程序的功能。
切换开关是动态生成的,它们运行自己的脚本,它可以工作。
我似乎无法解决的问题是,我添加了一个选项,该选项具有当脚本以 exit 1 结束以及当脚本转到 exit 1 时将切换开关保留在其真实位置的功能。exit 0 时,切换开关开关应从 true 更改为 false。 。 .
例如,这是一个脚本: “海豚;1号出口” 我单击切换开关 -> 它变为 true -> Dolphin 应用程序启动 -> 只要 Dolphin 打开,切换开关就保持 true -> 我关闭 Dolphin -> 切换开关应该返回到 false 位置,但问题是它不会返回错误位置
Plasma5Support.DataSource 的部分表示要带回切换开关“switchItem.checked = check;”不移动拨动开关,我该怎么做?
main.qml:
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support
PlasmoidItem {
id: root
readonly property var switches: JSON.parse(Plasmoid.configuration.switches)
readonly property bool hasSwitch: switches
Plasma5Support.DataSource {
id: executable
engine: "executable"
connectedSources: []
onNewData: function(sourceName, data) {
for (var i = 0; i < root.switches.length; ++i) {
var switchItem = root.switches[i];
if (switchItem.switchId === sourceName) {
switchItem.checked = data['exit code'] === 0;
} else {
if (switchItem.checked && data['exit code'] !== 0) {
switchItem.checked = checked;
}
}
}
disconnectSource(sourceName);
}
function exec(cmd) {
connectSource(cmd);
}
}
function toggleAction(switchId, checked) {
for (var i = 0; i < switches.length; ++i) {
var switchItem = switches[i];
if (switchItem.switchId === switchId) {
if (checked) {
executable.exec(switchItem.onScript);
} else {
executable.exec(switchItem.offScript);
}
break;
}
}
}
fullRepresentation: Item {
Repeater {
model: root.switches
delegate: PlasmaComponents3.Switch {
id: switchItem
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
checked: modelData.defaultPosition
onToggled: {
switchItem.checked = checked;
root.toggleAction(modelData.switchId, switchItem.checked);
}
}
}
}
}
}
这里是 plasmoid 的 Github,如果这有助于测试问题:https://github.com/xkain/Ultimat-ToggleSwitch
您的代码有两个问题:
开关被声明为类型
var
readonly property var switches
Repeater 的模型复制了
root.switches
一次
switches
的类型选择为 1. QML ListModel
、2. list<var>
、3. var
。前两个将为您提供对成员的更改检测,并且这会自动传播到中继器。最后一个没有,传播更改的唯一方法是继续分配 Repeater
模型,因为 QML 不会提供有关模型内更改的通知。