Score:1

Using GTK4 to build a small window app

gg flag

Once the small program is compiled, I run it by double clicking the executable in Nautilus. When I have a second but completely different windowed app and double click on it while the first windowed program is running, the result is that the first program is started for a second instance rather than a new instance of the different program.

Program 1:

#include <gtk/gtk.h>

//gcc $( pkg-config --cflags gtk4 ) -o example-0 example-0.c $( pkg-config --libs gtk4 ) -lGL -g

static void activate(GtkApplication* app, gpointer user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
  gtk_widget_show (window);
}

int main(int argc, char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

    printf("EXITING\n");
  return status;
}

Program 2:

//gcc $( pkg-config --cflags gtk4 ) -o example-1 example-1.c $( pkg-config --libs gtk4 ) 
#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_widget_set_halign (box, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (box, GTK_ALIGN_CENTER);

  gtk_window_set_child (GTK_WINDOW (window), box);

  button = gtk_button_new_with_label ("Hello World");

  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_destroy), window);

  gtk_box_append (GTK_BOX (box), button);

  gtk_widget_show (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

I cannot figure out why. Has anybody else ever ran across this problem? I should be easily reproducible.

I am on Ubuntu 22.04.2 LTS I have GTK v4 and the following compiler version:

$ gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

also using Wayland.

Thanks, Jaye

ru flag
Have you tested with straight X and not Wayland? It might be a Wayland related issue as Wayland introduces issues with some GUI applications (unfortunately). Just to rule out Wayland as the source of the problem/
JamworksPro avatar
gg flag
Yes I have. I have determined the problem is with the activate signal in GTK4. I have opened a ticket on gnome gitlab. Thanks though for your response!
Score:0
it flag

Two different GTK applications should have two different application_id. For example org.gtk.example1 and org.gtk.example2 in the gtk_application_new() instruction.

See https://docs.gtk.org/gio/type_func.Application.id_is_valid.html

David avatar
cn flag
In the original question it was answered and dealt with in the comments. I fail to see how this is an answer.
vmagnin avatar
it flag
I have tested your two examples and have the same problem (Kubuntu 22.10): trying to launch example-1 is launching example-0 a second time if example-0 is already running. Just use two different id strings in app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); and the problem is fixed.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.