[Solved] Problems changing model in combobox during runtime



  • Hi

    I have a table with 3 columns. Column 1 is just an index. Column 2 and 3 have comboboxes in each row. I want the content of combobox on a row in column 3 to change depending on the selection made in the other combobox on the same row. My main problem (I think) is that I am not able to access the id for the combobox. Below is an example where I try to do this using onCurrentIndexChanged. There is also a commented code part where I instead try doing this more directly when assigning the content to the model.

    Any suggestion is welcome...

    Cheers

    @ TableView {
    id: tv
    anchors.fill: parent

        ListModel {
            id: indexList
            ListElement { text: "1" }
            ListElement { text: "2" }
            ListElement { text: "3" }
        }
    
        ListModel {
            id: typeList
            ListElement { text: "Fruit" }
            ListElement { text: "Vegetables" }
        }
    
        ListModel {
            id: fruitList
            ListElement { text: "Apple" }
            ListElement { text: "Banana" }
            ListElement { text: "Orange" }
        }
    
        ListModel {
            id: vegList
            ListElement { text: "Carrot" }
            ListElement { text: "Salad" }
        }
    
        model: indexList
    
        TableViewColumn {
            title: "Index"
            role: "text"
        }
    
        TableViewColumn {
            id: type
            title: "Type"
    
            delegate: ComboBox {
                id: tcb
                width: parent.width
                height: parent.height
                model: typeList
                onCurrentIndexChanged: {
                    name.update(typeList.get(currentIndex).text)
                }
            }
        }
    
        TableViewColumn {
            id: name
            title: "Name"
            property var cbId
            function update(label) {
                console.log("Calling update "+label)
                if (label === "Fruit") {
                    ncb.model = fruitList
                }
                else if (label === "Vegetables"){
                    ncb.model = vegList
                }
            }
    
            delegate: ComboBox {
                id: ncb
                width: parent.width
                height: parent.height
                model: fruitList
    

    // model: tcb.currentindex === 0 ? fruitList : vegList

            }
        }
    }@

  • Moderators

    Hi,

    You are very much closer. You can use cbId of name to store the currentIndex's Text. Then just bind it to the model. Check this
    @
    onCurrentIndexChanged: {
    name.cbId = typeList.get(currentIndex).text
    }

    TableViewColumn {
    id: name
    title: "Name"
    property var cbId

    delegate: ComboBox {
        id: ncb
        currentIndex: name.cbId
        width: parent.width
        height: parent.height
        model: name.cbId === "Fruit" ? fruitList : vegList
    }
    

    }
    @



  • Hi!

    Thank you for the suggestion. When I try this, it updates the second combobox on all rows and not only the row "I am working on".

    I just now got some help on IRC #qt-quick... and after a bit back and forth I ended up with the code below. We also tried to assign currentIndex through roles (and e.g. styleData.value), but that didn't work out so well for me. Anyway, I got a solution I am happy with. :-)

    @TableView {
    id: tv
    anchors.fill: parent

        ListModel {
            id: indexList
            ListElement { text: "1"; typindex: 0; nameindex: 0 }
            ListElement { text: "2"; typindex: 1; nameindex: 1 }
            ListElement { text: "3"; typindex: 0; nameindex: 2 }
        }
    
        ListModel {
            id: typeList
            ListElement { text: "Fruit" }
            ListElement { text: "Vegetables" }
        }
    
        ListModel {
            id: fruitList
            ListElement { text: "Apple" }
            ListElement { text: "Banana" }
            ListElement { text: "Orange" }
        }
    
        ListModel {
            id: vegList
            ListElement { text: "Carrot" }
            ListElement { text: "Salad" }
        }
    
        model: indexList
    
        TableViewColumn {
            title: "Index"
            role: "text"
        }
    
        TableViewColumn {
            id: type
            title: "Type"
    
            delegate: ComboBox {
                id: tcb
                width: parent.width
                height: parent.height
                model: typeList
                currentIndex: indexList.get(styleData.row).typindex
                onCurrentIndexChanged: {
                    indexList.setProperty(styleData.row, "typindex", currentIndex)
                }
            }
        }
    
        TableViewColumn {
            id: name
            title: "Name"
            delegate: ComboBox {
                id: ncb
                width: parent.width
                height: parent.height
                currentIndex: indexList.get(styleData.row).nameindex
                model: indexList.get(styleData.row).typindex === 0 ? fruitList : vegList
            }
        }
    }@

  • Moderators

    Good one... Please mark the post as solved by editing the post title and prepend [solved], so others come to know.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.