4

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?
 }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
maxwell
  • 357
  • 4
  • 14
  • Do you want to access that value in a delegate? – eyllanesc May 07 '18 at 16:09
  • Is it possible to display data from this role without delegate? – maxwell May 07 '18 at 17:50
  • Also how about accessing the different columns? – maxwell May 07 '18 at 17:52
  • What do you know about the index ?, or before what action do you want to obtain that data ?. – eyllanesc May 07 '18 at 17:56
  • In principle (see edit) I want to be able to specify that a TableViewColumn should display the size of a file. This seems to be column 1 of the Qt::Displayrole – maxwell May 07 '18 at 19:13
  • in QML the idea of ​​row and column should not be used, the views instead use the roles. – eyllanesc May 07 '18 at 19:15
  • Ok, I understand. However, as you can see in the snippet above, the QFileSystemModel defined the size as column 1 of the Display role. How to display it then? A TableViewColumn with a dummy role and a delegate which accesses the information like in your answer? – maxwell May 07 '18 at 19:25
  • Exactly, that's what you should do, as the example of Qt shows: https://doc.qt.io/qt-5.10/qtquickcontrols-filesystembrowser-main-cpp.html – eyllanesc May 07 '18 at 19:29
  • Ok, this is the reason then. I already wondered why they do it like this if the could just use the info already stored in the model. – maxwell May 07 '18 at 19:40

4 Answers4

1

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

If you plan to reuse the model several times, you could consider sub-classing QFileSystemModel and add a custom role:

class FileSystemModel : public QFileSystemModel
{
public:

    explicit FileSystemModel(QObject *parent = nullptr) : QFileSystemModel(parent) {}

    enum Roles {
        FileSizeRole = Qt::UserRole + 1
    };

    QVariant data(const QModelIndex &index, int role) const
    {
        switch (role) {
        case FileSizeRole:
            return QFileSystemModel::data(this->index(index.row(), 1, index.parent()),
                                          Qt::DisplayRole);
        default:
            return QFileSystemModel::data(index, role);
        }
    }

    QHash<int, QByteArray> roleNames() const
    {
        auto result = QFileSystemModel::roleNames();
        result.insert(FileSizeRole, "fileSize");
        return result;
    }
};

This way, you can simply refer to the role by its name:

TreeView {
    model: fsModel
    anchors.fill: parent

    TableViewColumn {
        role: "display"
    }
    TableViewColumn {
        role: "fileSize"
    }
}
Martin Höher
  • 715
  • 5
  • 9
1

QFileSystemModel inherits from QAbstractItemModel, which has a method called roleNames(), that returns a QHash with the names of the default Roles (e.g. DysplayRole, DecorationRole, EditRole etc..) see:https://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames. To be accurate, QFileSystemModel defines its own roles on top of the QAbstracItemModel ones. see: https://doc.qt.io/qt-5/qfilesystemmodel.html#Roles-enum

So if you didn't define any custom role, then you can simply refer to the display role with it's default name (display) in your QML file . Like this:

TableView {
  model: fileSystemModel
 TableViewColumn {
   role: "display"
 }
}

That said, if you define custom roles, you have to override that roleNames() method, to give names to the new roles you defined. In that case, in order to keep consistency with the parent class, you should call first QAbstractItemModel::roleNames() method (in your case QFileSystemModel::roleNames()), and then set the new rolenames in the returned QHash. Here is an example for a login item where I defined host, username and password roles:

QHash<int, QByteArray> LoginModel::roleNames() const
{
    QHash<int,QByteArray> names = QAbstractItemModel::roleNames();
    names[HostRole] = "host";
    names[UsernameRole] = "username";
    names[PasswordRole] = "password";
    return names;
}
0

You can also simply use model.display or just display to get DisplayRole from any model.

tomas
  • 106
  • 4