checkbox value needs to change based on listview item
-
hi, i need some advice on this:
i have a checkbox outside of the listview, and its value depends on currentItem. I am having problem implementing it.
I did tried creating a global variable and change its value in onCurrentIndexChanged of the listview, but to no avail.
I also tried creating a signal which will be called in onCurrentIndexChanged and set the value of the global variable, but still to no avail.
I tried also to directly set the value of checkbox using the listview.currentItem.value.The checkbox value won't change after all.
Item { id: root property bool canEdit: false ListModel { id: viewModel ListElement { name: "menu" page: "MenuView.qml" editable: true } ListElement { name: "edit" page: "EditView.qml" editable: true } } ListView { id: viewList model: viewModel delegate: Item { Loader: { source: model.page } } onCurrentIndexChanged: { root.canEdit = model.get(currentIndex).editable; // i tried this when i created a global variable. } } Checkbox { //checked: viewList.model.get(viewList.currentIndex).editable; // i tried this to explicitly set the value checked: root.canEdit onCheckedChanged: { root.canEdit = checked; viewModel.setProperty(viewList.currentIndex, "editable", checked); } } }
Please advise.
-
Please post some code how do you attempt to check/ uncheck the checkbox.
-
Ah, it's QML. OK.
So the issue is probably that viewList.model.get() is a function, so it does not report changes (no notification signal) and QML engine does not know that the value has changed.
However, the approach with canEdit should have worked... unless CurrentIndex is changed at a different time than you think it is - add a console.log("I'm here!"+currentIndex); in onCurrentIndexChanged to see when it is really being invoked.
-
@sierdzio said in checkbox value needs to change based on listview item:
However, the approach with canEdit should have worked... unless CurrentIndex is changed at a different time than you think it is - add a console.log("I'm here!"+currentIndex); in onCurrentIndexChanged to see when it is really being invoked.
It is invoked when it started moving the listview item.
The problem is that when I tried ticking the box on one item, once you move to other item, it won't changed its (other item editable property) value, it will remain as checked.
EDIT:
It seems that the model didn't update directly after running setProperty, is there a way to force update/refresh the listModel? Because it gets updated after changing orientation only. -
@literA2 I didn't had the code for the QML files specified in your model. Here's an updated version as per your original example which works. Can you try it?
import QtQuick 2.0 Item { id: root width: 200 height: 200 property bool canEdit: false ListModel { id: viewModel ListElement { name: "menu" page: "MenuView.qml" editable: false } ListElement { name: "edit" page: "EditView.qml" editable: true } } ListView { id: viewList anchors.fill: parent model: viewModel delegate: Rectangle { width: parent.width height: 40 color: viewList.currentIndex == index ? "red" : "white" Text { anchors.centerIn: parent text: model.page } MouseArea { id: mouseArea anchors.fill: parent onClicked: viewList.currentIndex = index } } onCurrentIndexChanged: { root.canEdit = model.get(currentIndex).editable; } } CheckBox { anchors.bottom: parent.bottom checked: root.canEdit onCheckedChanged: { root.canEdit = checked; viewModel.setProperty(viewList.currentIndex, "editable", checked); } } }
Also please check whether your other QML files are causing any problem.