c++ - 如何访问 Qt::DisplayRole 并在 TableView 中指定列

标签 c++ qt qml qt5 qfilesystemmodel

QFileSystemModel 具有以下data 函数:

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);

我想知道如何访问 DisplayRole 并在 QML TableViewColumn 中指定我想要的列。

我想用在

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

最佳答案

如果您想在委托(delegate)中访问,您必须使用返回 QModelIndexstyleData.index 并将角色的值传递给它,在本例中为 Qt::DisplayRole 根据docs0:

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

如果知道父级的行、列和QModelIndex:

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

关于c++ - 如何访问 Qt::DisplayRole 并在 TableView 中指定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218276/

相关文章:

c++ - OMP 和 std::set<...>::iterator 上的并行操作

c++ - 如何禁用或隐藏 QFileDialog 中的默认取消按钮?

javascript - 使用 QWebChannel 将 qml 对象公开到网站/Javascript

javascript - 如何在 C++ 中创建对象并在 QML 中访问它

qt - 什么是 qmltestrunner?

c++ - 有没有办法查看 C++ 中的内置文档?

c++ - 指向槽函数的指针

c++ - 我如何链接到 OpenGL?

c++ - 从 QString 到 char 指针的转换生成空字符串

python - PyQT 中显示 QDialog 后执行代码的惯用方法是什么?