problem in getting folder icon from folder-path via QMime
Solved
General and Desktop
-
i found a code that gets icon of a file or folder by it's path .
here is the function .QIcon geticon(const QString &filename){ QIcon icon; QMimeDatabase mime_database; QList<QMimeType> mime_types = mime_database.mimeTypesForFileName(filename); for (int i=0; i < mime_types.count() && icon.isNull(); i++){ qDebug()<< mime_types[i].iconName(); icon = QIcon::fromTheme(mime_types[i].iconName()); } if (icon.isNull()) return QApplication::style()->standardIcon(QStyle::SP_FileIcon); else return icon; }
there is some problems in this function and i want to fix that
- it dose not detect if the given path is a folder path ,so it dose not return a folder icon.
- as i will give it a single file path , i don't want the "QList<QMimeType> mime_types " and the "for loop" in my code.
i tried different way to fix those but failed.
please help me. -
@saber If you don't want mime_types and the loop then use http://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile-1
-
Hi,
@saber said in problem in getting folder icon from folder-path via QMime:
mime_database.mimeTypesForFileName
From the looks of it, you should rather use QMimeDatabase::mimeTypeForFile.
-
@saber Please read again what I wrote.
Using http://doc.qt.io/qt-5/qmimedatabase.html#mimeTypeForFile-1 you don't need mime_types ... -
here is my solution
give it the file path it will return the icon.
QIcon geticon(const QString &filePath) { QIcon icon; QMimeDatabase mime; QMimeType mType; mType = mime.mimeTypeForFile(filePath); icon = QIcon::fromTheme(mType.iconName()); if (icon.isNull()) return QApplication::style()->standardIcon(QStyle::SP_FileIcon); else return icon; }