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

Access to item inside a GridView



  • I have a gridview with images. When an image is clicked a dialog appears, which is defined outside of the gridview. When the dialog is clicked, the state of the previously clicked image should be changed. How can I achieve this?

    Example code:

    @
    Rectangle {

    ListModel {
    id: gridModel
    ListElement{
    imageSource: ""
    }
    }

    Component {
    id: gridDelegate

    Item{
    Image {
    source: imageSource
    MouseArea{
    anchors.fill: parent
    onClicked: dialog.visible = true
    }
    }
    }
    }

    GridView {
    model: gridModel
    delegate: gridDelegate
    }

    Rectangle {
    id: dialog
    Button {
    onClick: {
    //change state of clicked image here//
    dialog.visible = false;
    }
    }
    }
    }
    @

    Somehow the spaces are messed up in the code exampe :/



  • @
    Rectangle {
    property int selectedIndex: -1

    ListModel {
     id: gridModel
     ListElement{
      imageSource: ""
     }
    }
     
    Component {
     id: gridDelegate
     
     Item{
      Image {
       source: imageSource
       MouseArea{
        anchors.fill: parent
        onClicked: dialog.visible = true
        selectedIndex = index
       }
      }
      function changeState() {
      }
     }
    }
     
    GridView {
        id: gridView
        model: gridModel
        delegate: gridDelegate
    }
     
    Rectangle {
     id: dialog
     Button {
      onClick: {
        //change state of clicked image here//
        dialog.visible = false;
        gridView.children[selectedIndex].changeState()
       }
     }
    }
    }
    

    @



  • Thx. Always feeling dumb, when a solution is that simple ;)


Log in to reply