QFileSystemModel custom icons?

前端 未结 2 492
情深已故
情深已故 2021-01-13 19:39

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 reim

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 20:06

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

提交回复
热议问题