Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Switch (and checkboxes) Not Following data model values



  • Hello. I am baffled by this. I created a super basic Qt Quick app as an example. I have a basic ListModel, ListView, and a Switch in the delegate. I added "All On" and "All Off" buttons. Clicking these buttons I can loop through the data model and easily flip all Switches to the checked position, whether true or false. The switches flip fine, back and forth. Here's the catch...
    If I go over and manually click one Switch to toggle it on and then back off, suddenly using my "All On" or "All Off" buttons begin ignoring that particular Switch. The others that I haven't touched still follow the actions of the button clicks, but those particular switches that I manually clicked either on or off are suddenly ignored by my javascript loop that edits the data model.

    Any ideas?

    import QtQuick 2.9
    import QtQuick.Controls 1.4
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        ListModel {
            id: boringModel
            ListElement{
                name: "Bob"
                mySwitchEnabled: false
            }
            ListElement{
                name: "Joe"
                mySwitchEnabled: false
            }
            ListElement{
                name: "Billy"
                mySwitchEnabled: false
            }
        }
    
        Column {
            id: btnCol
            x: 0
            y: 0
            height: parent.height
            width: 100
            Button {
                id: btnOn
                anchors.left: parent.left
                text: "All On"
                width: parent.width
                height: 20
                onClicked: {
                    console.log('clicked all on');
                    var myCount = boringModel.rowCount();
                    for (var i = 0; i < myCount; i++) {
                        var aListElement = boringModel.get(i);
                        aListElement.mySwitchEnabled = true;
                    }
                }
            }
            Button {
                id: btnOff
                anchors.left: parent.left
                text: "All Off"
                width: parent.width
                height: 20
                onClicked: {
                    console.log('clicked all off');
                    var myCount = boringModel.rowCount();
                    for (var i = 0; i < myCount; i++) {
                        var aListElement = boringModel.get(i);
                        aListElement.mySwitchEnabled = false;
                    }
                }
            }
        }
    
        ListView {
            id: switchView
            anchors.right: parent.right
            height: parent.height
            width: 200
            model: boringModel
            delegate: Rectangle {
                width: parent.width
                height: 30
                color: "#f2f2f2"
                Text {
                    id: myLabel
                    text: name
                    anchors.fill: parent
                }
                Switch {
                    id: mySwitch
                    anchors.right: parent.right
                    checked: mySwitchEnabled
                    height: parent.height
                    onClicked: {
                        console.log('clicked');
                    }
                }
            }
        }
    }
    


  • Any reason for Controls v1?

    This seems to be working fine here for me... what's the symptoms in the model you are speaking of? Visually everything works as expected / intuitive feel to me.

    I can toggle one an alternate value and then reset using the all's...

    The difference I have is:
    import QtQuick 2.10
    import QtQuick.Controls 2.3
    //import QtQuick.Controls 1.4



  • @6thC How interesting. I updated to 2.3 and works fine. I guess I'll just chalk it up to a bug in an earlier version. Thanks!


Log in to reply