Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

QML ListView Model setProperty Work only 1 time

I have a ListView filled with checkboxes, I want to change the checkbox status from a button click this is my code.

import QtQuick
import QtQuick.Controls 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ListView {
        id: mylist
        width: parent.width
        height: 300
        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
            bottom: change_state.top
            bottomMargin: 20
        }

        model: ListModel {
            id: mymodel
            ListElement {
                mystatus : false
            }
            ListElement {
                mystatus : false
            }
            ListElement {
                mystatus : false
            }
        }

        delegate: Rectangle {
            height: 50
            width: parent.width
            CheckBox {
                id: cb
                checked: mystatus
                anchors.fill: parent
                text: "State"
            }
        }
    }

    Button {
        id: change_state
        anchors {
            bottom: parent.bottom
        }
        text: "Change status"

        onClicked: {
            mymodel.setProperty(1, "mystatus", true)
            console.log("PRESSED")
        }
    }
}

The problem is when I press the button after running the app it changes the status and checks the box, but if I remove the check manually then press the button again it has 0 effects, never been able to check it again.

QT 6.3
Windows 10
same issue for Android, IOS, and Desktop.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Tested with QT 5.15.2 and the same issue.

>Solution :

When you click on the checkbox itself, you break the binding. Initially the checked property is bound to mystatus. But when you check the box manually, you’re forcing checked to be true or false, regardless of the state of mystatus.

You can keep the binding intact by catching the onClicked signal and updating mystatus, rather than forcing the checked value to change.

CheckBox {
    id: cb
    checked: mystatus
    anchors.fill: parent
    text: "State"

    onClicked: {
        mystatus = !mystatus
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading