QML TableView - How to get data of a row on mouse click?
by TheIndependentAquarius from LinuxQuestions.org on (#5AKFB)
Where do I write the mouseclick event and what would be the way to extract mouse clicked row data?
I looked in this thread: https://stackoverflow.com/questions/62633171/qml-tableview-get-row-clicked-with-qtquick-controls-2
I changed my MouseArea accordingly but it says that row is undefined.
Code:import QtQuick.Window 2.12
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.0
import QtQuick.Controls.Styles 1.4
Window
{
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TableView
{
anchors.fill: parent
style: TableViewStyle
{
headerDelegate: Rectangle
{
height: 20
color: "lightsteelblue"
Text {
width: parent.width
text: styleData.value
elide: Text.ElideMiddle
}
}
rowDelegate: Rectangle
{
color: "blue"
height: 30
MouseArea {
anchors.fill: parent
onClicked: {
console.log(row, column, mymodel.data(mymodel.model.index(row, column)))
}
}
}
}
TableViewColumn
{
role: "aaa"
title: "AAA"
width: 100
delegate: Item
{
Rectangle
{
anchors.left: parent.left
id: pic
radius: 100
height: 15; width: 15; color: "red"
}
Text
{
anchors.left: pic.right
anchors.leftMargin: 10
text: styleData.value
}
}
}
TableViewColumn
{
role: "bbb"
title: "BBB"
width: 100
}
model: ListModel
{
id: mymodel
ListElement
{
aaa : "Banana1"
bbb : "Apple1"
}
ListElement
{
aaa : "Banana2"
bbb : "Apple2"
}
}
}
}


I looked in this thread: https://stackoverflow.com/questions/62633171/qml-tableview-get-row-clicked-with-qtquick-controls-2
I changed my MouseArea accordingly but it says that row is undefined.
Code:import QtQuick.Window 2.12
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.0
import QtQuick.Controls.Styles 1.4
Window
{
visible: true
width: 640
height: 480
title: qsTr("Hello World")
TableView
{
anchors.fill: parent
style: TableViewStyle
{
headerDelegate: Rectangle
{
height: 20
color: "lightsteelblue"
Text {
width: parent.width
text: styleData.value
elide: Text.ElideMiddle
}
}
rowDelegate: Rectangle
{
color: "blue"
height: 30
MouseArea {
anchors.fill: parent
onClicked: {
console.log(row, column, mymodel.data(mymodel.model.index(row, column)))
}
}
}
}
TableViewColumn
{
role: "aaa"
title: "AAA"
width: 100
delegate: Item
{
Rectangle
{
anchors.left: parent.left
id: pic
radius: 100
height: 15; width: 15; color: "red"
}
Text
{
anchors.left: pic.right
anchors.leftMargin: 10
text: styleData.value
}
}
}
TableViewColumn
{
role: "bbb"
title: "BBB"
width: 100
}
model: ListModel
{
id: mymodel
ListElement
{
aaa : "Banana1"
bbb : "Apple1"
}
ListElement
{
aaa : "Banana2"
bbb : "Apple2"
}
}
}
}