Search code examples
c++qtqmlqt5qfilesystemmodel

How to access Qt::DisplayRole and specify columns in TableView


The QFileSystemModel has the following data function:

Variant QFileSystemModel::data(const QModelIndex &index, int role) const
{
    Q_D(const QFileSystemModel);
    if (!index.isValid() || index.model() != this)
        return QVariant();

    switch (role) {
    case Qt::EditRole:
    case Qt::DisplayRole:
        switch (index.column()) {
        case 0: return d->displayName(index);
        case 1: return d->size(index);
        case 2: return d->type(index);
case 3: return d->time(index);

I wonder how I can access the DisplayRole and specify the column I want in a QML TableViewColumn.

I want to use it in

TableView {
  model: fileSystemModel
 TableViewColumn {
   role: //what comes here?
 }
}

Solution

  • If you want to access within a delegate you have to use styleData.index that returns the QModelIndex and pass it the value of the role, in this case Qt::DisplayRole that according to the docs is 0:

    view.model.data(styleData.index, 0)
    

    if you know the row, column and QModelIndex of parent:

    view.model.data(view.model.index(row, colum, ix_parent), 0)