PDA

View Full Version : How to get file type Icon ?



wirasto
10th July 2010, 14:19
So far, I get icon file like this


QIcon Olongia::fileIcon(const QString &filename)
{
QFileInfo info(filename);
const QString ext=info.suffix().toLower();

QIcon icon;
if (ext == "pdf") {
icon=QIcon::fromTheme("application-pdf");
} else if (ext.startsWith("doc")) {
icon=QIcon::fromTheme("application-msword");
} else if (ext=="zip" ||
ext=="gz" ||
ext=="bz2" ) {
icon=QIcon::fromTheme("application-x-archive");
} else if (ext=="png" ||
ext=="jpg" ||
ext=="gif" ||
ext=="svg" ||
ext=="bmp") {
icon=QIcon::fromTheme("image-x-generic");
} else {
QProxyStyle s;
icon=s.standardIcon (QStyle::SP_FileIcon);
}

return icon;
}


I think that is not efficient, and after read qtdoc I get new trick like this


QFileInfo info(filename);
QFileIconProvider ip;
QIcon icon=ip.icon(info);

But, that code only working if file is exists in local system. And I need get file type icon for file not exists in local system ? Like QFtp example, only get the filename and show in the TreeViewWidget ?

Thank's before and sorry about my suck english :)

borisbn
10th July 2010, 16:54
You can create temporary empty file with wanted extention and go with way, you described

wirasto
11th July 2010, 13:16
Nice idea. I will try :)

wysota
11th July 2010, 13:20
How about taking a look at the source code of QFileIconProvider to see how it does it and do the same without having a real file?

wirasto
12th July 2010, 13:18
You can create temporary empty file with wanted extention and go with way, you described

This trick work on GNOME and Windows. But not on KDE :)