1

Ok, I know about the class QFileIconProvider. I also know about QMimeType (and QMimeDatabase). But I am surprised that I cannot find a simple connection between the two.

So what I need is to get an icon used by the operating system for a given type of files. Normally, this is done like this:

QIcon icon = QFileIconProvider().icon(QFileInfo("/path/to/some.file"));

But what if I want to get an icon without reference to a particular file, just for a given MIME type? Say, my application uses attached files and syncs across multiple devices; on some devices the attached file might be absent, but I know the MIME type of that file, so I still want my app to show the proper file icon, using only the MIME type info.

As a temporary solution, I found that if I pass a non-existing file to QFileInfo in the code string above, it does retrieve a correct icon (obviously, purely from the extension of that non-existing file), at least in Linux. But this method is not documented, and there is no guarantee it will always work.

So I need something that could work as the non-existing method QFileIconProvider().icon(QMimeType &).

Maximko
  • 627
  • 8
  • 20
  • [`QMimeType::iconName`](http://doc.qt.io/qt-5/qmimetype.html#iconName-prop) might possibly be of use. – G.M. Aug 18 '18 at 18:34
  • Yeah, I've seen that. But the only way to use the return value of this function is to pass it to `QIcon::fromTheme()` which, however, works only in X11, and my app is supposed to be cross-platform (in fact, even in Xubuntu 16.05 this function returns a string like "application-pdf", but `QIcon::fromTheme()` does not produce a valid icon). – Maximko Aug 18 '18 at 18:39
  • Well, not quite. It only works by *default* on `X11` systems. But as stated in the docs. if you want to use it on other platforms "...you will have to bundle a compliant theme in one of your themeSearchPaths() and set the appropriate themeName()." So it's extra work but certainly (I think) do-able. – G.M. Aug 18 '18 at 18:48

0 Answers0