0

I tried to derive a model from QFileSystemModel, added a CheckBox to the delegate displayed in a TreeView and wanted to save the state of the checkbox in the Qt::CheckStateRole. However, Why is it not possible to use the Qt::CheckStateRole in a QFileSystemModel? I added the CheckStateRole to my derived model:

QHash<int,QByteArray> roleNames() const Q_DECL_OVERRIDE
{
     QHash<int, QByteArray> result = QFileSystemModel::roleNames();
     result.insert(UrlStringRole, QByteArrayLiteral("urlstring"));
     result.insert(SizeRole, QByteArrayLiteral("size"));
     result.insert(LastModifiedRole, QByteArrayLiteral("lastModified"));
     result.insert(IsDirRole, QByteArrayLiteral("isDir"));
     result.insert(Qt::CheckStateRole,QByteArrayLiteral("checkState"));
     return result;
}

but when in qml I do

 console.log('checkstaterole',model.checkState)

in my delegate in qml, it says 'checkstaterole undefined'.

Why is it not possible to use the CheckStateRole in a QFileSystemModel? I thought every model derived from QAbstractItemModel has this role.

numberCruncher
  • 595
  • 1
  • 6
  • 25
  • Hm, `QAbstractItemModel::roleNames()` doesn't seem to be a virtual function. And is `checkState` a model's function? – vahancho May 04 '18 at 09:02
  • it is virtual, see http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames (a bit hidden to the right). I thought you can access the models roles in qml via model.roleName – numberCruncher May 04 '18 at 09:05
  • Ok, I just checked the implementation of qfilesystemmodel. it simply does not return anything for the Qt::CheckStateRole – numberCruncher May 04 '18 at 09:10
  • @numberCruncher see https://stackoverflow.com/questions/50178597/how-to-add-a-custom-role-to-qfilesystemmodel/50180682#50180682, It seems that the 2 are working on the same. :P – eyllanesc May 04 '18 at 22:21
  • thanks for the post – numberCruncher May 05 '18 at 07:57
  • @numberCruncher If that answer helps you, do not forget to give it an upvote :D – eyllanesc May 05 '18 at 17:05

0 Answers0