Re: gtk_main_quit() as button callback?

If you're calling g_application_run() then you don't need to call gtk_main() as well: the run() method will spin the main loop for you.

You also don't use gtk_main_quit() to stop the application's main loop: you should use g_application_quit() instead.


On Wednesday, July 22, 2015, richard boaz <ivor boaz gmail com> wrote:

I'm in the process of converting my app from v2 to v3 and have come across something strange:

Attaching gtk_main_quit() to a button's click callback does not result in the application quitting.

What I see:
  1. attaching gtk_main_quit() to the to the top window destroy signal works fine, i.e., quit the app using the WM 'x'
  2. attaching gtk_main_quit() to a button does not work:
    2.1) first click - nothing happens
    2.2) each subsequent click - warning message is written to the cmd line:

        gtk_main_quit: assertion 'main_loops != NULL' failed

  3. gdb tells me that gtk_main_quit() is correctly invoked on the button click
  4. I will also deduce from the warning message that the main loop is actually gone after the first click
So why does gtk_main() not return?

This worked as expected in GTK2.

The following minimal program (top window + quit button) demonstrates this strangeness.  Could this be platform-specific somehow?  (Ubuntu 14.04)

any insights appreciated,


== quit button example ==
// save to quit.c
// compilation:
// gcc quit.c `pkg-config --cflags --libs gtk+-3.0` -g -o quit-button

#include <gtk/gtk.h>

static void activate(GtkApplication *app, gpointer nil)
GtkWidget    *topWindow, *button;
topWindow = gtk_application_window_new(app);
gtk_container_set_border_width(GTK_CONTAINER(topWindow), 2);

g_signal_connect(topWindow, "destroy", G_CALLBACK(gtk_main_quit), NULL);
button = gtk_button_new_with_label("QUIT");
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(topWindow), button);
gtk_window_resize(GTK_WINDOW(topWindow), 300, 350);
gtk_window_set_position(GTK_WINDOW(topWindow), GTK_WIN_POS_CENTER_ALWAYS);

int main(int argc, char **argv)
int status = 0;
GtkApplication *app;
  app = gtk_application_new("quit.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);

return status;

[ ] ebassi []

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]