How to make QAbstractTableModel with qml tableview checkable
-
Hello,
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
@
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 FilterRole: return model.name(); 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: trueTableViewColumn { role: "name" title: "Name" width: 200 } TableViewColumn { role: "description" title: "Description" width: 100 } TableViewColumn { role: "filter" title: "Filter" width: 100 } TableViewColumn { role: "type" title: "Type" width: 100 }
@
-
Hi,
IMO, you must create your own delegate with a CheckBox and Text elements and set the CheckBox accordingly. See "itemDelegate":http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop
-
Hi,
Thank you for your help. I used a delegate checkbox like this :
@
Component {
id: checkBoxDelegateItem { CheckBox { anchors.fill: parent checked: styleData.value } } }
@
@
TableViewColumn {
role: "check"
title: ""
width: 30
delegate: checkBoxDelegate
}
@And I modified data like this:
@
if(role == CheckRole) {
return rowsChk.contains(index.row()) ? Qt::Checked : Qt::Unchecked;
}
@But when I clicked on checkbox, data never updated.