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. Change the ListModel dynamically

Change the ListModel dynamically

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.1k 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    @
    import QtQuick 2.1
    import QtQuick.Controls 1.0

    Rectangle {
    id: root
    width: 500
    height: 500

    state: "one"
    
    ListModel{
        id: one
    }
    
    ListModel{
        id: two
    }
    
    function changeModel(){
        if(state == "one"){
            one.append({"source": "aaa"})
            return one
        }else{
            two.append({"source": "bbb"})
            return two
        }
    }
    
    Row{
        Button{
            id: buttonOne
    
            text: "One"
    
            onClicked: {
                root.state = "one"
            }
        }
    
        Button{
            id: buttonTwo
    
            text: "Two"
    
            onClicked: {
                root.state = "two"
            }
        }
    
        TableView{
            id: tableView
            model: changeModel()
    
            TableViewColumn {                
                title: "image"
    
                delegate: Text{ text: tableView.model.get(styleData.row).source }
            }
        }
    }
    

    }

    @

    When I click the "Two" button, there are always an error message

    qrc:/main.qml:59: TypeError: Cannot read property 'source' of undefined

    How could I change the model dynamically?What kind of mistake I make?Thanks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JapieKrekel
      wrote on last edited by
      #2

      The reason is that you change your underlaying model of the TableView and both are not of the same length.
      Since you use .get(styleData.row) all the delegates visible in the table refer to a specific element on index (row number) in you model.
      If you change the model, each delegate will try to get its data again, but the number of items of the other model is different, and if the number of items is less than the items that are visible, some of the items will fail to get its data, those rows will dissappear but give an error.
      If you use:
      @#
      delegate: Text{ text: styleData.value }
      @

      instead, then it works, without errors, but then you cannot have multiple properties in the same ListElement.

      Hope it helps.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        Thanks for your help:).Do we have a more flexible way to switch between different models?

        Solution one :
        Design a intermediate models(called C) with several properties
        when I switch the model, I could clear the data of C
        and copy the data of different models(A or B) to C

        Solution two :
        Use Loader to load the view, when the model change
        destroy the old table and load a new table

        Do you have easier solutions?Thanks a lot

        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