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. ListView Model setProperty Work only 1 time
Forum Updated to NodeBB v4.3 + New Features

ListView Model setProperty Work only 1 time

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 199 Views 1 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by AmrCoder
    #1

    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, so what i did do wrong?

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

    Update:
    Tested with QT 5.15.2 and the same issue.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemons
      wrote on last edited by
      #2

      per default the binding of the checkboxes checked property will break once you manually click the checkbox.
      to preserve the binding, simply do sth. like this:

      CheckBox {
          id: cb
          checked: mystatus
          anchors.fill: parent
          text: "State"
          // in newer Qt versions this should be sufficient
          onClicked: {
              mystatus = checked
          }
          // otherwise additionaly use Connections or Qt.binding to preserve the binding
      }
      
      1 Reply Last reply
      2
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        In addition to what @lemons said, model property is not updated when you changed the check box from UI. Hence the issue. You can try with following piece of code inside the CheckBox.

                    onClicked: {
                        _root.ListView.view.model.setProperty(1, "mystatus",checked)
                    }
        

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1

        • Login

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