How to make checked a toggleswitch in a repeater outside the repeater?

hi,

I’m trying to create a plasmoid for KDE Plasma 6 that has the function of launching scripts/applications via dynamically generated toggle switches.

The toggleswitchs are generated dynamically and they run their own scripts, it’s ok, it works.

The problem that I cannot solve is that I added an option which has the function of toggling the toggleswitch outside of the repeater

For example, here is a script:
“dolphin; exit 1”
I click the toggle switch → it becomes true → the Dolphin application launches → the toggle switch stays on as long as Dolphin is open → I close Dolphin → the toggle switch should return to disabled position but the problem is that it does not return to disabled position

The part of Plasma5Support.DataSource that says to bring back the toggleswitch “switchItem.checked = checked;” doesn’t move the toggleswitch, how can I do that?

import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami

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);
                    switchItem.checked = false;
                } else {
                    executable.exec(switchItem.offScript);
                }
                break;
            }
        }
    }
    
    Plasmoid.backgroundHints: PlasmaCore.Types.DefaultBackground | PlasmaCore.Types.ConfigurableBackground
    
    preferredRepresentation: hasSwitch ? fullRepresentation : compactRepresentation
    fullRepresentation: Item {
        GridLayout {
            readonly property bool isVertical: {
                switch (Plasmoid.formFactor) {
                    case PlasmaCore.Types.Planar:
                    case PlasmaCore.Types.MediaCenter:
                    case PlasmaCore.Types.Application:
                        return root.height > root.width;
                    case PlasmaCore.Types.Vertical:
                        return true;
                    case PlasmaCore.Types.Horizontal:
                        return false;
                }
            }
            width:  isVertical ? root.width : implicitWidth
            height: isVertical ? implicitHeight : root.height
            flow: isVertical ? GridLayout.TopToBottom : GridLayout.TopToBottom
            columnSpacing: Kirigami.Units.smallSpacing
            rowSpacing: Kirigami.Units.smallSpacing
            
            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);
                    }
                }
            }
            
            PlasmaComponents3.Button {
                visible: !root.hasSwitch
                text: "Configure…"
                onClicked: Plasmoid.internalAction("configure").trigger()
            }
        }
    }
}




here is the Github of the plasmoid if that helps for testing the problem : GitHub - xkain/Ultimat-ToggleSwitch: Plasmoid switch configurable to launch scripts

If anyone can help, thanks in advance

You can use the itemAt() function to access individual child items of the Repeater, and call it in a loop, looking for the switch that meets a certain criterion.

Is a switch really the right UI element to use here, though? These are typically used for things with an on/off state; I would think it’s a bit weird for an app to launch when you click a switch.

Hi,

Launching the application with the toggleswitch was just for the example.
In the first idea it is to activate scripts with (script to turn on LEDs for example), as long as the script is activated the toggleswitch is activated and when the script is finished the toggleswitch should deactivate position because the script is finished.
(I try to use exit 1 and exit 0 to know the status of the script, it works but not to return the toggle switch to its disabled position).
The logic is therefore always for on/off even if the possibilities are wider

I had already seen for itemAt() but I don’t know how to apply it in my case, very complicated for me, I’m trying, I’m a beginner (first month learning to code) this plasmoid is a way to 'learn for me.

*Thank you for your time and for all your work on KDE!