I've found a solution, albeit with a small bit of help from ChatGPT as a last resort. I used an if/else statement (in the form of a ternary operator) for my main.qml and added an additional @Slot() decorator for the LED off function in my main,py.
.
.
↓ main.qml ↓
import QtQuick
import QtQuick.Controls
ApplicationWindow {
width: 1920
height: 1080
visible: true
// Bool property to hold the button state
property bool buttonState: false
// Function to toggle the LEDs
function toggleLEDs() {
buttonState ? controlPanel.buttonOff() : controlPanel.buttonOn();
buttonState = !buttonState;
}
Image {
id: allLights
source: "images/power_" + (buttonState ? "on" : "off") + ".png"
anchors.centerIn: parent
MouseArea {
anchors.fill: parent
onClicked: {
toggleLEDs();
}
}
}
}
.
.
↓ main,py ↓
import sys
from pathlib import Path
from led import grid, all_lights_on, all_lights_off
from PySide6.QtCore import QObject, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
class ControlPanel(QObject):
def __init__(self):
super().__init__()
@Slot()
def buttonOn(self):
all_lights_on(grid)
@Slot()
def buttonOff(self):
all_lights_off(grid)
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
controlPanel = ControlPanel()
engine.rootContext().setContextProperty("controlPanel", controlPanel)
qml_file = Path(__file__).parent / 'main.qml'
engine.load(qml_file)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())