QFileSystemModel custom icons?
Asked Answered
C

2

5

In my project, I have a QTreeView displaying a location on my drive. I need to change all the icons of the files to a custom icon but leave the folders alone.

I reimplemented QFileSystemModel and I was able to change ALL the icons. Any way to limit the change to only files instead of folders?

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

This:

enter image description here

Becomes:

enter image description here

How can I only change the icon of the files?

Thanks for your time :)

Cigarillo answered 21/12, 2014 at 5:41 Comment(0)
C
3

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);
}
Cigarillo answered 21/12, 2014 at 6:28 Comment(0)
A
0

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
}
Antin answered 21/12, 2014 at 5:49 Comment(1)
I added toString() to index.data() for it to compile. The result did not change any of the icons. It looks like the first image I linked in my post.Cigarillo

© 2022 - 2024 — McMap. All rights reserved.