本文介绍了QFileSystemModel自定义图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我有一个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);
}
这:
成为: p>
>
如何只能更改文件的图标?
感谢您的时间:)
解决方案
我回答了自己的问题:
QVariant MyQFileSystemModel :: data (const QModelIndex& index,int role)const {
if(role == Qt :: DecorationRole)
{
QFileInfo info = MyQFileSystemModel :: fileInfo
if(info.isFile())
{
if(info.suffix()==dat)
return QPixmap File); //根据扩展选择图标
else if(info.suffix()==mcr)
return QPixmap(:/ icons / Region_Icon.png );
}
}
return QFileSystemModel :: data(index,role);
}
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:
Becomes:
How can I only change the icon of the files?
Thanks for your time :)
解决方案
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);
}
这篇关于QFileSystemModel自定义图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!