问题
I use Glade to make GUI. I did a main window and a dialog. The dialog is called from a button placed in the main window. The problem is that when I close (delete-event) the dialog it is impossible to call it again with the same button click of the main window. I am able to hide the dialog withgtk_widget_hide_on_delete
, but this option is not what I need. I need to close the dialog and free the memory it takes.
So what is the proper way to destroy the dialog and call it again with the same button click?
The way I've implemented in c++:
#include <gtk/gtk.h>
#include <stdlib.h>
GtkBuilder *gtkBuilder;
GtkWidget *main_window;
GtkWidget *grid_dialog;
GtkToolButton *grid_toolbutton;
void grid_any_clicked(GtkWidget *widget,
gpointer user_data)
{
GtkWidget *window = (GtkWidget *) user_data;
gtk_widget_show_all(window);
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
gtkBuilder = gtk_builder_new();
// GUI
gtk_builder_add_from_file(gtkBuilder, "../GUI.glade", NULL);
// Widgets
main_window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "main_window"));
grid_dialog = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "grid_dialog"));
grid_toolbutton = GTK_TOOL_BUTTON(gtk_builder_get_object(gtkBuilder, "grid_toolbutton"));
// Events
g_signal_connect(grid_toolbutton, "clicked", G_CALLBACK(grid_any_clicked), grid_dialog);
gtk_builder_connect_signals(gtkBuilder, NULL);
// Finalizing
g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show_all(main_window);
gtk_main();
return 0;
}
回答1:
The delete-event
is for when you want to intercept the fact that the user clicked on the window close button, and you want to react to this action. Think of editors that ask you if you want to save an document with unsaved changes before closing the editor.
What you want is the destroy
signal, inherited from GtkWidget.
Just don't connect to delete-event
, as calling destroy
is the default behavior, or return FALSE from delete-event
so the closing event propagates and hits destroy
.
来源:https://stackoverflow.com/questions/46519999/how-to-properly-close-a-dialog-made-in-glade