Reputation: 1
i am trying to make the Linux GTK Dialog Close after clicking the exit Button, any help?
void on_linux_exit_clicked()
{
// gtk_main_quit (); << this closes entire Program
// gtk_window_close (GtkWindow* window);
//gtk_window_close (GtkWindow *window);
// gtk_window_close(window);
// gtk_window_close();
// gtk_window_close GTK_WINDOW(window);
}
void on_network_and_computer_clicked(){
// create a window with four buttons with the text "Connection Configuration", "Connection Creation", "Computer Name", "Linux Network Settings"
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_title(GTK_WINDOW(window), "Network and Computer Settings");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
GtkWidget *btn_settings_dialog = gtk_button_new_with_label("Network Connection Configuration");
GtkWidget *btn_new_con = gtk_button_new_with_label("Network Connection Creation");
GtkWidget *btn_computerName = gtk_button_new_with_label("Edit Computer Name");
GtkWidget *btn_linux_network_settings = gtk_button_new_with_label("Linux Network Settings");
GtkWidget *btn_linux_exit = gtk_button_new_with_label("EXIT");
gtk_box_pack_start(GTK_BOX(vbox), btn_new_con, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), btn_settings_dialog, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), btn_computerName, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), btn_linux_network_settings, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), btn_linux_exit, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
// the clicked handler function respectively are: 'on_btn_settings_dialog_clicked', 'on_btn_new_con_clicked', 'on_computerName_clicked', 'on_ntpUpdate_clicked'
g_signal_connect(btn_settings_dialog, "clicked", G_CALLBACK(on_btn_settings_dialog_clicked), NULL);
g_signal_connect(btn_new_con, "clicked", G_CALLBACK(on_btn_new_con_clicked), NULL);
g_signal_connect(btn_computerName, "clicked", G_CALLBACK(on_computerName_clicked), NULL);
g_signal_connect(btn_linux_network_settings, "clicked", G_CALLBACK(on_networking_clicked), NULL);
g_signal_connect(btn_linux_exit, "clicked", G_CALLBACK(on_linux_exit_clicked), NULL);
gtk_widget_show_all(window);
}} //Extern `
Everything that is Commented out i have tried to no avail.
Upvotes: 0
Views: 118
Reputation: 15
To close the GTK Dialog window upon clicking the exit button, you can use the following code within your on_linux_exit_clicked()
function:
void on_linux_exit_clicked(GtkButton *button, gpointer user_data) {
GtkWidget *window = gtk_widget_get_toplevel(GTK_WIDGET(button));
gtk_widget_destroy(window);
}
gtk_widget_get_toplevel(GTK_WIDGET(button))
: This function retrieves the top-level window containing the button that triggered the signal.
gtk_widget_destroy(window)
: This function destroys the specified widget (in this case, the top-level window), effectively closing it.
Replace your existing on_linux_exit_clicked()
function with this code, and your GTK Dialog window should close properly upon clicking the exit button.
Upvotes: 0
Reputation: 372
without reproducible code it's hard to say, a window-close can be written as
void close_window_on_click(GtkWidget *widget, gpointer data) {
if (GTK_IS_WINDOW(data)) gtk_window_close(GTK_WINDOW(data));
}
...
g_signal_connect(some_button, "clicked", G_CALLBACK(close_window_on_click), some_window);
Upvotes: 0