I am trying to get the icon of an app (doesn't matter which one). I noticed Qt doesn't have something like GDesktopAppInfo
and therefore I tried getting it through QSettings
from /usr/share/applications/appname.desktop
. That's already a problem, because the desktop file might not be there. Anyway, going further to extract the Icon
key. Now I dunno how to find the url (notice that I need the url, sure I could make a QIcon
, but I need to export it to QML, which would mean another QQuickImageProvider
class, anyway, I don't wanna go that way). Is it possible, or is the aforementioned QQuickImageProvider
my only solution?
Asked
Active
Viewed 1,352 times
0

user2563892
- 553
- 1
- 7
- 16
-
What applications are you takling about? Your own application? All installed application on the users's computer? What is you input data (e.g. a list of app names)? – Simon Warta Dec 23 '14 at 19:15
-
@SimonWarta Not my own, other apps (music apps). All that I have is the desktop entry's name (that's all really, incredible, no?) – user2563892 Dec 23 '14 at 19:32
2 Answers
2
Here is a little guide that might help you find your way. Keep one thing in mind: start with the basic case, get code running and extend it to more difficult cases later.
For now, lets assume the following:
- .desktop file is in
/usr/share/applications
- App icon is in SVG or PNG format
- App icon path is absolute
- App name is lower case and does not contain whitespace
Input: App name "git-cola"
- Read
/usr/share/applications/git-cola.desktop
- Use a QRegularExpression to get the
Icon
value - You get an absolute
iconPath
, e.g./usr/share/git-cola/icons/git.svg
- Have an invokable C++ function that exposes a
QUrl
to QML - In QML, set the
source
property of anImage
togetIconUrl("Target App")
where 4. looks something like
QUrl MyClass::getIconUrl(QString appName)
{
// get iconPath from appName
return QUrl::fromLocalFile(iconPath);
}
If things are running, you can add support for
- Multiple .desktop locations (there might be a handful or so)
- Add support for relative paths
- Add support for XPM files

Community
- 1
- 1

Simon Warta
- 10,850
- 5
- 40
- 78
-
-
@user2563892 From following steps 1.-3. implemented in your C++ code – Simon Warta Dec 24 '14 at 17:44
-
An icon property doesn't necessarily have an absolute url, it might just be the icon name in the theme i.e. the one installed – user2563892 Dec 24 '14 at 18:05
-
@user2563892 As I said, start with the easiest case. If you don't find an example app using an absolute URL, edit the .desktop file by hand to make it absolute. When that is running properly you can add support for icon names. You make your life easier when you split your problem into small pieces. – Simon Warta Dec 25 '14 at 08:50
-
Well, the problem is, I target the music players. Most of them are pre-installed or available in the official repositories and therefore they install the icons in the right place. Moreover I noticed that every music player on my system uses icon names, so it would be more efficient to start with that case – user2563892 Dec 25 '14 at 08:57
-
Well then find a way to get from icon names to icon paths. Hint: Have a look at `/usr/share/pixmaps` – Simon Warta Dec 25 '14 at 09:02
-
Ok I had a look. I was still unable to find at least 1 icon for a music player. I am really looking forward to an alternative GDesktopAppInfo or I am just gonna use QIcon() cause it finds the icons itself and just hope that the .desktop file is in /usr/share/applications – user2563892 Dec 25 '14 at 09:21
0
You can use QIcon::fromTheme(QString iconName) to find the icon. It works most of the time but it's not as reliable as gtk

Harindu Dilshan
- 174
- 12