【问题标题】:QFileSystemModel custom icons?QFileSystemModel 自定义图标?
【发布时间】:2015-02-19 15:04:46
【问题描述】:

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

这个:

变成:

如何只更改文件的图标?

感谢您的宝贵时间:)

【问题讨论】:

    标签: c++ qt icons qfilesystemmodel


    【解决方案1】:

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

    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
    }
    

    【讨论】:

    • 我将 toString() 添加到 index.data() 以进行编译。结果没有改变任何图标。它看起来像是我在帖子中链接的第一张图片。
    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2013-11-10
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2021-11-18
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多