Unable to set icon for gtk4 application

Following are the things I have followed:

# cat /usr/share/applications/one.two.desktop 
[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
Icon=one.two
StartupNotify=true
Terminal=false

icon file is /usr/share/icons/one.two.png

#include <gtk/gtk.h>

GtkWidget *window = NULL;

static void activate( GtkApplication *app, gpointer user_data )
{
	window = gtk_application_window_new( app );
	GtkIconTheme *theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
	gtk_icon_theme_add_resource_path (theme, "/usr/share/icons/" );
	// icon image file is /usr/share/icons/one.two.png
	gtk_window_set_default_icon_name( "one.two" );
	gtk_widget_show( window );
}

int main( int argc, char **argv )
{
	GtkApplication *app = gtk_application_new ("one.two", G_APPLICATION_FLAGS_NONE);
	if( g_signal_connect( app, "activate", G_CALLBACK( activate ), NULL ) <= 0 )
	{
		fprintf( stderr, "Unable to connect\n" );
		exit(1);
	}
	int status = g_application_run( G_APPLICATION( app ), argc, argv);
	g_object_unref( app );
	return status;
}

executed using

$ gcc icon.c `pkg-config --cflags --libs gtk4` -o one_two
$ ./one_two 

icon is not showing up. Not sure where the error is. Can someone please help me understanding how to fix?

Using Icon=name means you’re using a named icon from the icon theme. Putting an icon under /usr/share/icons is not the same.

You should follow the documentation: Integrating with GNOME

Thanks for the quick reply. I did the following modifications

$ ls /usr/share/icons/hicolor/48x48/apps/one.two.png
/usr/share/icons/hicolor/48x48/apps/one.two.png
$ ls /usr/share/icons/hicolor/256x256/apps/one.two.png 
/usr/share/icons/hicolor/256x256/apps/one.two.png
$ ls /usr/share/icons/hicolor/scalable/apps/one.two.svg 
/usr/share/icons/hicolor/scalable/apps/one.two.svg
$ ls /usr/share/icons/hicolor/symbolic/apps/one.two-symbolic.svg 
/usr/share/icons/hicolor/symbolic/apps/one.two-symbolic.svg
$ cat /usr/share/applications/one.two.desktop 
[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
StartupNotify=true
Terminal=false

Still I am not seeing icon.
Size of png icon is also 256x256 pixels
size of svg icon is 320x320 pixels

can some one help me understanding where the issue please…

[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
StartupNotify=true
Terminal=false

Your desktop file is missing the Icon key:

Icon=one.two

You should also run gtk-update-icon-cache and update-desktop-database at installation time; Meson has a post-install command for that:

gnome = import('gnome')
gnome.post_install(
  gtk_update_icon_cache: true,
  update_desktop_database: true,
)
$ cat /usr/share/applications/one.two.desktop 
[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
Icon=one.two
StartupNotify=true
Terminal=false
$ sudo gtk-update-icon-cache 
$ sudo update-desktop-database 
$ ls /usr/share/icons/hicolor/48x48/apps/one.two.png
/usr/share/icons/hicolor/48x48/apps/one.two.png
$ ls /usr/share/icons/hicolor/256x256/apps/one.two.png 
/usr/share/icons/hicolor/256x256/apps/one.two.png
$ ls /usr/share/icons/hicolor/scalable/apps/one.two.svg 
/usr/share/icons/hicolor/scalable/apps/one.two.svg
$ ls /usr/share/icons/hicolor/symbolic/apps/one.two-symbolic.svg 
/usr/share/icons/hicolor/symbolic/apps/one.two-symbolic.svg

$ ./one_two 

Still not working…!!!

Does gnome-shell actually list “one two Application” as application when you search for it? If not, it might be because there is no one_two binary in its $PATH, but that’s just a guess because you are using ./one_two to run the application. In that case either install it to one of the $PATH directories used by gnome-shell or specify the full path in the Exec key.

1 Like
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
$ sudo cp one_two /usr/bin/
$ sudo gtk-update-icon-cache 
$ sudo update-desktop-database 
$ one_two 
$ 

still not working… :cold_sweat:

Does gnome-shell list the application when you search for it?

I just noticed your desktop file is missing the required Type key, so it is probably not showing up in gnome-shell. You can use desktop-file-validate to find problems like this.

Yes.
I typed one_ and pressed tab. two has been filled.

Hope it is the same thing you asked me.

I meant:

  1. press Windows/Super key
  2. type “one two”

Does the app then show up in the search results then?

$ cat /usr/share/applications/one.two.desktop 
[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
Icon=one.two
Type=Application
StartupNotify=true
Terminal=false

SOLVED…!!!

@skeller @ebassi

Thanks a lot.

just consolidating the solution

code:

#include<gtk/gtk.h>

GtkWidget *window = NULL;

static void activate( GtkApplication *app, gpointer user_data )
{
	window = gtk_application_window_new( app );
	GtkIconTheme *theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
	gtk_icon_theme_add_resource_path (theme, "/usr/share/icons/" );
	gtk_window_set_default_icon_name( "one.two.png" );
	gtk_widget_show( window );
}

int main( int argc, char **argv )
{
	GtkApplication *app = gtk_application_new ("one.two", G_APPLICATION_FLAGS_NONE);
	if( g_signal_connect( app, "activate", G_CALLBACK( activate ), NULL ) <= 0 )
	{
		fprintf( stderr, "Unable to connect\n" );
		exit(1);
	}
	int status = g_application_run( G_APPLICATION( app ), argc, argv);
	g_object_unref( app );
	return status;
}

compile the application :

$ gcc icon.c `pkg-config --cflags --libs gtk4` -o one_two

put the application in $PATH :

sudo cp one_two /usr/bin

provide the .desktop file

$ cat /usr/share/applications/one.two.desktop 
[Desktop Entry]
Name=one two Application
Comment=Learning application
Exec=one_two
Icon=one.two
Type=Application
StartupNotify=true
Terminal=false

Validate desktop file just to find the missing keys.

$ desktop-file-validate /usr/share/applications/one.two.desktop 

place the icon at right folder

$ ls /usr/share/icons/one.two.png 
/usr/share/icons/one.two.png

update icon cache/database

$ sudo gtk-update-icon-cache 
$ sudo update-desktop-database 

Finally launch the application $one_two.
Icon Shows up.
Hope this helps new learners.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.