I’m new in QT. I want to make every row of qml tableview checkable but it doesn’t work.
The tableview with data is shown successfully but it’s not checkable.
It seems that flags() and setData() functions are never run and role==Qt::CheckStateRole never be true.
Please help.
C++ code
I modified code like but it doesn't work:
QVariant TableModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= _fields->size())
return QVariant();
if(role == Qt::CheckStateRole) {
return rowsChk.contains(index.row()) ? Qt::Checked : Qt::Unchecked;
}
switch(role) {
case NameRole:
return model.name();
case DescriptionRole:
return model.description();
case TypeRole:
return model.type();
}
return QVariant();
}
bool TableModel::setData(const QModelIndex & index, const QVariant & value, int role){
rowsChecked(index.row(), value) ;
emit dataChanged(index, index);
return true;
}
here is my qml file
TableView {
model: tableModel
anchors.fill: parent
frameVisible: true
headerVisible: true
sortIndicatorVisible: false
alternatingRowColors: true
Component {
id: checkBoxDelegate
Item {
CheckBox {
anchors.fill: parent
checked: styleData.value
}
}
}
TableViewColumn {
role: "check"
title: ""
width: 30
delegate: checkBoxDelegate
}
TableViewColumn {
role: "name"
title: "Name"
width: 200
}
TableViewColumn {
role: "description"
title: "Description"
width: 100
}
TableViewColumn {
role: "type"
title: "Type"
width: 100
}