Gtk - close window form with button in C

I have a main window with a menu that opens another window. This secondary window has a Close button. This button has a connected signal. My problem is that I do not know how to close / destroy this parent window. I tried with gtk_widget_destroy, but an error appears because the window is not widgets .... I did not find any function to destroy the parent window ....

Can someone show me the way please? Thanks in advance.

-----------------------------------------------

Ok I am posting a piece of code. When I run the program, I click the "Open window" button. A new window is opened with one "Close" button. If I click the Close button, I will get the following error in the terminal: (Windows: 13801): Gtk-CRITICAL **: gtk_widget_destroy: statement `GTK_IS_WIDGET (widget) 'failed

Code:

#include <stdlib.h> #include <gtk/gtk.h> #include <gdk/gdkkeysyms.h> void open_window(GtkWidget *widget, gpointer window); void close_window(GtkWidget *widget, gpointer window); int main( int argc, char *argv[]) { GtkWidget *window; GtkWidget *fixed; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "Windows"); gtk_window_set_default_size(GTK_WINDOW(window), 230, 150); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window), fixed); button = gtk_button_new_with_label("Open window"); gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50); gtk_widget_set_size_request(button, 80, 35); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(open_window), G_OBJECT(window)); g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0; } void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); gtk_builder_connect_signals (builder, NULL); g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } void close_window(GtkWidget *widget, gpointer window) { gtk_widget_destroy(GTK_WIDGET(window)); } 

The file "secondWindow.glade" defines the window, table and button placed in the middle cell of the table. In addition, it is defined by the handle of the clicked button with the name close_window.

Link to the glade file if someone wants to execute it: https://sites.google.com/site/marvalsiteimages/secondWindow.glade

Hope this helps you understand my problem. Thansk.

----------------------------------------------- -

Result based code based on answer:

 void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; GtkWidget *closeButton = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton")); g_signal_connect (G_OBJECT (closeButton), "clicked", G_CALLBACK (close_window), G_OBJECT (secondWindow)); // here is the magic: the callback will get the window to close g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } 
+4
source share
1 answer

Your problem is that the signal to β€œclick” the buttons of the second window is associated with the file glade. But the signal handler needs a window pointer to destroy it. This is passed through the "user_data" parameter of the signal callback.

One way is to pass the second window as the user_data argument to Glade (check out this Glade tutorial ), but the argument should be a pointer, and I don't know how this can be done with a glade. EDIT : just click on the user data field associated with this signal in the meadow, and a pop-up window will allow you to select an object to transmit to the signal handler. Just select your "secondWindow" object.

Another way to do this is to simply remove the signal processing from the glade file and manually connect the clicked signal from the code, passing the pointer to the second window as user data:

 void open_window(GtkWidget *widget, gpointer window) { GtkBuilder *builder; GtkWidget *secondWindow = NULL; GtkWidget *closeButton = NULL; builder = gtk_builder_new (); gtk_builder_add_from_file (builder, "secondWindow.glade", NULL); secondWindow = GTK_WIDGET (gtk_builder_get_object (builder, "secondWindow")); closeButton = GTK_WIDGET (gtk_builder_get_object (builder, "closeWindowButton")); g_signal_connect (G_OBJECT (closeButton), "clicked", G_CALLBACK (close_window), G_OBJECT (secondWindow)); // here is the magic: the callback will get the window to close g_object_unref (G_OBJECT (builder)); gtk_window_set_modal(GTK_WINDOW(secondWindow), TRUE); gtk_widget_show_all(secondWindow); } 
+4
source

Source: https://habr.com/ru/post/1436499/


All Articles