QFileSystemModel custom icons?

ぃ、小莉子 提交于 2019-12-01 10:59:58

I answered my own question:

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

Try to get filename and check is it a file or not. So it should be something like that:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!