QML ListView模型setProperty仅工作1次

发布于 2025-01-17 22:03:36 字数 1442 浏览 3 评论 0原文

我有一个充满复选框的 ListView,我想通过单击按钮更改复选框状态,这是我的代码。

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")
        }
    }
}

问题是,当我在运行应用程序后按下按钮时,它会更改状态并选中该框,但如果我手动删除选中然后再次按下按钮,它的效果为 0,永远无法再次选中它。

QT 6.3 Windows 10 Android、IOS 和桌面版也存在同样的问题。

使用 QT 5.15.2 进行测试并出现相同的问题。

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.

Tested with QT 5.15.2 and the same issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-01-24 22:03:36

当您单击复选框本身时,您就中断了绑定。最初,checked 属性绑定到 mystatus。但是,当您手动选中该框时,您将强制 checked 为 true 或 false,无论 mystatus 的状态如何。

您可以通过捕获 onClicked 信号并更新 mystatus 来保持绑定完整,而不是强制更改 checked 值。

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

    onClicked: {
        mystatus = !mystatus
    }
}

更新:

对于取消选中所有复选框的添加问题,您应该执行与上面相同的操作并更新模型中的 mystatus 值,而不是直接触摸 checked 属性,以便绑定不会被破坏。

for (var i = 0; i < mymodel.count; ++i) {
    mymodel.setProperty(i, "mystatus", false);
}

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
    }
}

UPDATE:

For your added question of unchecking all the boxes, you should do the same thing as above and update the mystatus value in the model rather than directly touching the checked property so that the binding doesn't get broken.

for (var i = 0; i < mymodel.count; ++i) {
    mymodel.setProperty(i, "mystatus", false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文