C++ QFileSystemModel自定义图标?

C++ QFileSystemModel自定义图标?,c++,qt,icons,qfilesystemmodel,C++,Qt,Icons,Qfilesystemmodel,在我的项目中,我有一个QTreeView,显示驱动器上的位置。我需要将文件的所有图标更改为自定义图标,但不要使用文件夹 我重新实现了QFileSystemModel,并且能够更改所有图标。有没有办法将更改仅限于文件而不是文件夹 QVariant MyQFileSystemModel::data(const QModelIndex& index, int role) const { if(role == Qt::DecorationRole) return QPix

在我的项目中,我有一个QTreeView,显示驱动器上的位置。我需要将文件的所有图标更改为自定义图标,但不要使用文件夹

我重新实现了QFileSystemModel,并且能够更改所有图标。有没有办法将更改仅限于文件而不是文件夹

QVariant MyQFileSystemModel::data(const QModelIndex& index, int role) const
{
    if(role == Qt::DecorationRole)
        return QPixmap(":/icons/TAG_Int.png");
    return QFileSystemModel::data(index, role);
}
这:

变成:

如何仅更改文件的图标


谢谢您的时间:)

尝试获取文件名并检查它是否为文件。所以应该是这样的:

QVariant MyQFileSystemModel::data(const QModelIndex& index, int role) const
{
    if(role == Qt::DecorationRole)
    {
        QString name = index.data();//get filename
        QFileInfo info(name);       
        if(info.isFile())           //check
            return QPixmap(":/icons/TAG_Int.png");//return image if file
    }
    return QFileSystemModel::data(index, role);   //if not, standard processing
}

我回答了自己的问题:

QVariant MyQFileSystemModel::data( const QModelIndex& index, int role ) const {
    if( role == Qt::DecorationRole )
    {
        QFileInfo info = MyQFileSystemModel::fileInfo(index);

        if(info.isFile())
        {
            if(info.suffix() == "dat")
                return QPixmap(":/icons/File_Icon.png");//I pick the icon depending on the extension
            else if(info.suffix() == "mcr")
                return QPixmap(":/icons/Region_Icon.png");
        }
    }
    return QFileSystemModel::data(index, role);
}
我将toString()添加到index.data()中,以便对其进行编译。结果没有改变任何图标。这看起来像是我在帖子中链接的第一张图片。