1
votes

I'm trying to get data from a specific column when the row is selected.

For exemple, when the user selects the first row, I need to retrieve the value of the column "Image source". Many thanks!!

main.qml

 TableView {
     onClicked: {
         console.log("value "+ column2.value )
      }        
 id:tablev

 model: mySQLmodel
 anchors.margins: 12
 anchors.fill: parent


 TableViewColumn {               
   id: column1
   role: "Title"
   title: "Title"
    width: 120
}

TableViewColumn {
   id: column2
   role: "Credit"
   title: "Credit"
   width: 120
 }

TableViewColumn {
  id: column14
  role: "Image"
  title: "Image source"
  width: 120
  }

itemDelegate: Text
{
  text: styleData.value
  elide: Text.ElideRight
 } }}}

enter image description here

3

3 Answers

3
votes

Assume you are using TableView with model.

ListModel {
   id: libraryModel
   ListElement{ title: "A title 1" ; credit: "N/A"; source: "http://someurl.com" }
   ListElement{ title: "A title 2" ; credit: "N/A"; source: "http://someurl.com" }
   ListElement{ title: "A title 3" ; credit: "N/A"; source: "http://someurl.com" }

}

TableView {
   TableViewColumn{ role: "title"  ; title: "Title" ; width: 100 }
   TableViewColumn{ role: "credit" ; title: "Credit" ; width: 200 }
   TableViewColumn{ role: "source" ; title: "Image source" ; width: 200 }
   model: libraryModel
}

So you can add onClick event to the view in that way:

onClicked: {
    console.log(libraryModel.get(row).source);
} 

when row is the selected row passed to onClicked by engine

0
votes

[Solved]

I use the function bellow and works great:

QVariantMap Model::get(int idx) const {
QVariantMap map;
foreach(int k, roleNames().keys()) {
map[roleNames().value(k)] = data(index(idx, 0), k);
}
return map;
}

Happy codding :)

0
votes

You could add something like: `

TableView {
    itemDelegate: Item  {
        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log("row: " + styleData.row + " column: " + styleData.column + " value: " + styleData.value)
            }
        }
    }
}

`