cell index of tableview on click
-
Hi everyone,
I managed to get it. Here is a piece of my code.TableView { id: logslist anchors.fill: parent anchors.topMargin: header.height columnSpacing: 0 rowSpacing: 0 clip: true model: ChangesTableModel {} boundsBehavior: Flickable.StopAtBounds onWidthChanged: logslist.forceLayout() MouseArea { anchors.fill: parent onClicked: { console.log(logslist.cellAtPos(mouseX, mouseY)) } } columnWidthProvider: function (column) { return Math.max(80, logslist.model ? logslist.width/logslist.model.columnCount() : 0) } delegate: Rectangle { id:mycell implicitWidth: logslist.columnWidthProvider(column) implicitHeight: celltext.height border.color: "gray" border.width: 1 color: choosenTheme < 1 ? "white" : "#303030" //color: (heading === true) ? "red" : "green" Text { id: celltext padding: 2 text: tabledata verticalAlignment: Text.AlignVCenter elide: Text.ElideRight color: choosenTheme < 1 ? "black" : "white" font.pixelSize: 12 } } ScrollBar.vertical: ScrollBar {} ScrollBar.horizontal: ScrollBar {} }
The key is this line: console.log(logslist.cellAtPos(mouseX, mouseY))
It returns the correct values as long as I don't scroll the table view. When I scroll it then the result is -1, -1. Accoring to Qt documentaction:
"Returns the cell at the given position in the view. If no cell intersects with position, the return value will be point(-1, -1)"
Any advice how to solve it?
-
Hi, I solved the problem in a different way. I past my code, if it can help anybody.
import QtQuick import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 import QtQuick.Controls.Material 2.15 import ChangesTableModel 1.0 Item { id: mainview anchors.fill: parent Row { id: header width: logslist.contentWidth height: 50 x: -logslist.contentX z: 1 spacing: 0 Repeater { model: ["Here", "are", "my", "headers", ":-)"] Rectangle { width: 80 height: parent.height color: "orange" Text { anchors.centerIn: parent wrapMode: Text.WrapAtWordBoundaryOrAnywhere text: modelData } } } } TableView { id: logslist anchors.fill: parent anchors.topMargin: header.height columnSpacing: 0 rowSpacing: 0 clip: true model: ChangesTableModel {} boundsBehavior: Flickable.StopAtBounds onWidthChanged: logslist.forceLayout() columnWidthProvider: function (column) {return 80} delegate: Rectangle { id:mycell implicitWidth: logslist.columnWidthProvider(column) implicitHeight: celltext.height border.color: "gray" border.width: 1 color: choosenTheme < 1 ? "white" : "#303030" //color: (heading === true) ? "red" : "green" Text { id: celltext padding: 2 text: tabledata verticalAlignment: Text.AlignVCenter elide: Text.ElideRight color: choosenTheme < 1 ? "black" : "white" font.pixelSize: 12 } MouseArea { anchors.fill: parent onClicked: { console.log("cell[" + mycell.x / mycell.implicitWidth + "," + mycell.y / mycell.implicitHeight + "]") } } } ScrollBar.vertical: ScrollBar {} ScrollBar.horizontal: ScrollBar {} } }
The solution was to move the MouseArea from the TableView to the delegate: Rectangle and then calculate the cell from their position and with and height.
-
This post is deleted!
-
Hi, I solved the problem in a different way. I past my code, if it can help anybody.
import QtQuick import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 import QtQuick.Controls.Material 2.15 import ChangesTableModel 1.0 Item { id: mainview anchors.fill: parent Row { id: header width: logslist.contentWidth height: 50 x: -logslist.contentX z: 1 spacing: 0 Repeater { model: ["Here", "are", "my", "headers", ":-)"] Rectangle { width: 80 height: parent.height color: "orange" Text { anchors.centerIn: parent wrapMode: Text.WrapAtWordBoundaryOrAnywhere text: modelData } } } } TableView { id: logslist anchors.fill: parent anchors.topMargin: header.height columnSpacing: 0 rowSpacing: 0 clip: true model: ChangesTableModel {} boundsBehavior: Flickable.StopAtBounds onWidthChanged: logslist.forceLayout() columnWidthProvider: function (column) {return 80} delegate: Rectangle { id:mycell implicitWidth: logslist.columnWidthProvider(column) implicitHeight: celltext.height border.color: "gray" border.width: 1 color: choosenTheme < 1 ? "white" : "#303030" //color: (heading === true) ? "red" : "green" Text { id: celltext padding: 2 text: tabledata verticalAlignment: Text.AlignVCenter elide: Text.ElideRight color: choosenTheme < 1 ? "black" : "white" font.pixelSize: 12 } MouseArea { anchors.fill: parent onClicked: { console.log("cell[" + mycell.x / mycell.implicitWidth + "," + mycell.y / mycell.implicitHeight + "]") } } } ScrollBar.vertical: ScrollBar {} ScrollBar.horizontal: ScrollBar {} } }
The solution was to move the MouseArea from the TableView to the delegate: Rectangle and then calculate the cell from their position and with and height.