4

在我的项目中,我有一个 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);
}

这个:

在此处输入图像描述

变成:

在此处输入图像描述

我怎样才能只更改文件的图标?

谢谢你的时间 :)

4

2 回答 2

2

我回答了我自己的问题:

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);
}
于 2014-12-21T06:28:09.223 回答
0

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

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
}
于 2014-12-21T05:49:32.107 回答