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. Set model on every PropertyChanges
Forum Updated to NodeBB v4.3 + New Features

Set model on every PropertyChanges

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 257 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.
  • NiltavaN Offline
    NiltavaN Offline
    Niltava
    wrote on last edited by
    #1

    In the example snippet below, when User clicks on button1 the state changes to 'State-A' and the model is set for the ListView (list), displaying the list of 'fruits'. When User clicks on button2 the state changes to 'State-B'. This is causing the ListView to disappear however the count of items in the ListView is still 5.

    Why does this happen?

    Do I need to set the model on every PropertyChanges ?

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4

    Window {

    visible: true
    width: Math.round(Screen.width/2)
    height: Math.round(Screen.height/2)
    
    Item {
        id: root
        anchors.fill: parent
    
        states: [
            State {
                name: "State-A"
                PropertyChanges {
                    target: list
                    model: fruits
                }
                StateChangeScript {
                    name: "s1"
                    script: {
                        console.log("current state is " + root.state)
                    }
                }
            },
            State {
                name: "State-B"
                StateChangeScript {
                    name: "s2"
                    script: {
                        console.log("current state is " + root.state + "; list count=" + list.count)
                    }
                }
            }
    
        ]
    
        Button {
            id: button1
            anchors.left: root.left
            anchors.top: root.top
            anchors.margins: 10
            text: "Change to State-A"
            onClicked: root.state = "State-A"
        }
    
        Button {
            id: button2
            anchors.left: button1.right
            anchors.top: root.top
            anchors.margins: 10
            text: "Change to State-B"
            onClicked: root.state = "State-B"
        }
    
        ListModel {
            id: fruits
            ListElement { name: "Orange" }
            ListElement { name: "Apple" }
            ListElement { name: "Banana" }
            ListElement { name: "Kiwi" }
            ListElement { name: "Mango" }
        }
    
        Rectangle {
            id: rect
            anchors.centerIn: root
            width: 180
            height: 150
            border.color: "darkblue"
            border.width: 2
            color: "beige"
    
            ListView {
                id: list
                anchors.fill: rect
                anchors.margins: 10
                delegate: Text {
                    text: name
                }
            }
        }
    }
    

    }

    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