Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Gtk3 quit application from a menu item fails

Im trying to

  • call g_application_quit from a menu item to quit the application same as the x button
  • using signal connect :

g_signal_connect(quitMi, "activate",
G_CALLBACK(g_application_quit),G_APPLICATION(app));

  • but I get the following message and application stays open

message

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(quit:XXX): GLib-GIO-CRITICAL **: 09:59:42.547: g_application_quit: assertion 'G_IS_APPLICATION (application)' failed

code

#include <gtk/gtk.h>

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

  GtkWidget *menubar;
  GtkWidget *fileMenu;
  GtkWidget *fileMi;
  GtkWidget *quitMi;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "chesstrainer");

  gtk_container_set_border_width (GTK_CONTAINER (window), 0);

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  menubar = gtk_menu_bar_new();
  fileMenu = gtk_menu_new();
  fileMi = gtk_menu_item_new_with_label("File");
  quitMi = gtk_menu_item_new_with_label("Quit");

  gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
  gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quitMi);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
//  gtk_container_add (GTK_CONTAINER (window), frame);
  gtk_container_add (GTK_CONTAINER (vbox), frame);
  g_signal_connect(quitMi, "activate",
      G_CALLBACK(g_application_quit),G_APPLICATION(app));

  drawing_area = gtk_drawing_area_new ();
  /* set a minimum size */
  gtk_widget_set_size_request (drawing_area, 800, 800);

  gtk_container_add (GTK_CONTAINER (frame), drawing_area);

  gtk_widget_show_all (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;
}

How can I properly close the application from the quit menu item ?

>Solution :

If you read the documentation, the activate signal for a GtkMenuItem requires a callback with the signature void (*)(GtkMenuItem *mi, gpointer userdata).

You are passing in g_application_quit, which has a signature of void (*)(GApplication *).

Hence the error message, indicating the the argument to g_application_quit is not a GApplication *, since it is receiving a GtkMenuItem *.

You need to write a callback for the activate signal, and have it call g_application_quit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading