Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. checkbox value needs to change based on listview item
Forum Updated to NodeBB v4.3 + New Features

checkbox value needs to change based on listview item

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 3.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    literA2
    wrote on last edited by literA2
    #1

    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.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please post some code how do you attempt to check/ uncheck the checkbox.

      (Z(:^

      L 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Please post some code how do you attempt to check/ uncheck the checkbox.

        L Offline
        L Offline
        literA2
        wrote on last edited by
        #3

        @sierdzio I just updated the description. Thanks.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          L 1 Reply Last reply
          0
          • sierdzioS sierdzio

            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.

            L Offline
            L Offline
            literA2
            wrote on last edited by literA2
            #5

            @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.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              literA2
              wrote on last edited by literA2
              #6

              It seems that the problem is the checkbox won't update its checked value even if the canEdit is already set to false.

              Any ideas in resolving this? Thanks.

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @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.

                157

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved