0

I have TableView filled with text. One of columns has checkbox as delegate. I check one of checkboxes. How can I get row index of current checkbox?

To get row index I use currentRow function but it returns -1 because when I check checkbox I don't select row.

Is there any other way to get checkbox row index?

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
Pillar
  • 87
  • 2
  • 11
  • Post the QML code, in particular the delegate – Ispas Claudiu Nov 18 '16 at 15:54
  • Check this thread on Qt forum, it's similar to what you want to do: http://www.qtforum.org/article/36990/returning-row-s-number-after-clicking-qpushbutton.html – Mo Abdul-Hameed Nov 18 '16 at 23:22
  • Like all other values associated with the cell through `styleData? as the answer from @folibis shows. Not sure how you even got the displaying to work without `styleData` – Kevin Krammer Nov 19 '16 at 10:36

1 Answers1

2

There too much possibilities to do that. Some very simple example of one of them:

import QtQuick 2.7
import QtQuick.Window 2.0
import QtQuick.Controls 1.4


Window {
    visible: true
    width: 300
    height: 200

    TableView {
        id: table
        anchors.fill: parent
        signal checked(int row, int col, bool isChecked)
        TableViewColumn { role: "col1"; title: "Columnt1"; width: 100 }
        TableViewColumn { role: "col2"; title: "Columnt2"; width: 100 }
        TableViewColumn { role: "col3"; title: "Columnt3"; width: 100 }
        model: ListModel {
            ListElement { col1: true; col2: false; col3: false }
            ListElement { col1: false; col2: false; col3: true }
            ListElement { col1: true; col2: false; col3: true }
        }
        itemDelegate: Item {
            CheckBox {
                anchors.centerIn: parent
                checked: styleData.value
                onClicked: {
                    table.selection.clear();
                    table.selection.select(styleData.row);
                    table.currentRow = styleData.row;
                    table.checked(styleData.row, styleData.column, checked);
                }
            }
        }
        onChecked: console.log(row, col, isChecked);
    }
}
folibis
  • 12,048
  • 6
  • 54
  • 97