c++ - 如何通过自定义 QAbstractItemModel 在 QTableView 中显示图标?

标签 c++ qt model

我正在构建自定义 QAbstractItemModel 模型。

第一列包含图标,第二列 - 文本。

这是数据方法的代码:

QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
{
    if(role != Qt::DisplayRole )
        return QVariant();

    int col = index.column();
    if (col == 0)
    {
        return iconProvider->icon(QFileIconProvider::Folder);
    }
    else if (col == 1)
    {
        return "TEXT";
    }
}

但我在生成的 TableView 中得到的只是第二列中的文本。第一列中没有文件夹图标。

我是不是漏掉了什么?

最佳答案

Qt::DisplayRole 仅适用于文本。添加:

if ( role == Qt::DecorationRole ) {
    return iconProvider->icon(QFileIconProvider::Folder);
}

关于c++ - 如何通过自定义 QAbstractItemModel 在 QTableView 中显示图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10820919/

相关文章:

c++ - 打印链表只打印头节点

c++ - 将 int 转换为 base 2 cstring/string

c++ - 编译 QDbus 服务器应用程序时出错?

c++ - 在 CPP 中将时间转换为 unix 时间

python - zinnia django 中的 django.contrib.comments.moderation.AlreadyModerated 错误

matlab - 训练新的 LatentSVMDetector 模型

c++ - 求大 n 和 k 模 m 的二项式系数

android - eclipse 错误 : "Path must include project and resource name" on invoking clean

c++ - Qt ListView 不显示 C++ 模型内容

ruby-on-rails - Rails 中的模型类型和排序?