error-handlinggtk

Why GTK stops running of the rest of program?


I have a C++ program that I used this simple gtk code at first of my main() function, and my goal is my app shows an image when it started and keeping showing the image and keep the rest of program.

  GtkWidget* window;
  GtkWidget* image1;
  GtkWidget* image2;

  gtk_init (NULL,NULL);


  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  image1  = gtk_image_new_from_file("1.jpg");
  image2  = gtk_image_new_from_file("2.jpg");

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);


  g_signal_connect(G_OBJECT (window), "destroy",
             G_CALLBACK (destroy), NULL);

  gtk_container_add(GTK_CONTAINER (window), image1);

  gtk_widget_show_all(window);

  gtk_main();

THE REST OF PROGRAM THAT WONT EXECUTE!

But when it opens a window and shows the image, it stuck there and doesn't execute the rest of code! Why this happens?


Solution

  • After you execute gtk_main, your code will "block", if i can say that, in that line until gtk_main_quit it's called/executed.

    This is the nature of graphical user interfaces. Typically, you setup everything, call the main loop and wait for user interaction.

    I would suggest that you read GNOME's Getting Started with GTK+ (Archived).

    As an example, if you do any printf below gtk_main, it will get printed after you close the GtkWindow.

    Your application logic must be defined previously, then by means of user interaction, pressing buttons and other widgets, the application will do "things".

    When you call gtk_main, the main loop starts.

    The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts.

    You can read more about it here : The Main Event Loop (Archived).