Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

change the content of a listview



  • Hello,
    I would like to change the content of the preview by a green rectangle by clicking on an element of the listview. By default the black rectangle should always be displayed.
    Thank you

    //code
    ScrollView{
    id:scrollview
    clip: true
    width: colonne_1_2.width/1.2
    height: colonne_1_2.height/3
    /width:parent.width
    height: parent.height
    /
    ScrollBar.vertical.policy:ScrollBar.AlwaysOn
    anchors{
    left: parent.left
    //leftMargin: parent.height * 0.05
    top: parent.top
    topMargin: parent.height * 0.09
    }

                        ListView{
                            id:modellist
                           // x:120
                           // y:40
                            width: 60
                            height: 200
                            anchors.centerIn: parent
                            highlight: Rectangle{
                                anchors.fill: parent
                                //color: "grey"
    
                            }
                            focus: true
                            orientation: Qt.Vertical
                            model:ListModel{
                                id: nameModel
                                ListElement{
                                    name:"Template BMD Simple"
                                }
                                ListElement{
                                    name:"element 1"
                                }
                                ListElement{
                                    name:"element 2"
                                }
                                ListElement{
                                    name:"element 3"
                                }
                                ListElement{
                                    name:"element 4"
                                }
    

    }
    delegate:nameDelegate

    Component {
    id: nameDelegate
    Rectangle {
    id: top
    color: ListView.isCurrentItem ? "royalblue" : "white"
    width:parent.width/1.2
    height: text.implicitHeight
    Text {
    id: text
    anchors.centerIn: parent
    text:name
    color: parent.ListView.isCurrentItem ? "white" : "black"
    font.pixelSize: 14
    MouseArea {
    anchors.fill: parent
    hoverEnabled: true
    onClicked: top.ListView.view.currentIndex = model.index
    }
    }
    }
    }
    // Below is the listview whose content I want to change by clicking on an element
    ListView {
    id: list
    width: preview.width
    height: preview.height

                        model: controller // model is read from Column's model
    
                        currentIndex: modellist.currentIndex
                        //highlightMoveDuration: 200
    
                        delegate:ColumnLayout{
                            Rectangle{
                                id:contenue2
                                width: preview.width
                                height: preview.height
                                color: "black"
                            }
                            Rectangle{
                                id:contenue3
                                width: preview.width
                                height: preview.height
                                color: "green"
                            }
                        }
    

    }


Log in to reply