GTK4: Main window minimizes after I clicked on my save button

I am developing an application in C using GTK4 where a helper window (add_book_window) is opened from the main window to add books.

void on_add_book_clicked(GtkButton *button, gpointer user_data)
{   
    CoreWindow *win = CORE_WINDOW(user_data);

    GtkBuilder *builder = gtk_builder_new_from_resource("/org/gtk/coreapp/add_book_window.ui");
    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "add_book_window"));
    
    g_signal_connect(save_button, "clicked", G_CALLBACK(on_save_button_clicked), win);

    g_object_set_data(G_OBJECT(save_button), "title_entry", title_entry);
    g_object_set_data(G_OBJECT(save_button), "author_entry", author_entry);
    g_object_set_data(G_OBJECT(save_button), "isbn_entry", isbn_entry);

    //gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(win)); 
    gtk_window_set_modal(GTK_WINDOW(window), TRUE);
    gtk_window_present(GTK_WINDOW(window));

    g_object_unref(builder);
}

The helper window contains text fields (GtkEntry) to enter the title, author and ISBN of the book. However , if I interact with the save book button, and close the auxiliary window, the main window is automatically minimized. If I don’t interact with the button, the problem does not occur.

My save button function:

void on_save_button_clicked(GtkButton *button, gpointer user_data)
{
    CoreWindow *win = CORE_WINDOW(user_data);
    GtkWidget *add_book_window = GTK_WIDGET(gtk_widget_get_ancestor(GTK_WIDGET(button),GTK_TYPE_WINDOW));

    GtkEntry *title_entry = GTK_ENTRY(g_object_get_data(G_OBJECT(button), "title_entry"));
    GtkEntry *author_entry = GTK_ENTRY(g_object_get_data(G_OBJECT(button), "author_entry"));
    GtkEntry *isbn_entry = GTK_ENTRY(g_object_get_data(G_OBJECT(button), "isbn_entry"));

    const char *title = gtk_editable_get_text(GTK_EDITABLE(title_entry));
    const char *author = gtk_editable_get_text(GTK_EDITABLE(author_entry));
    const char *isbn = gtk_editable_get_text(GTK_EDITABLE(isbn_entry));
   
    if (!check_lenght_save_button(title, author, isbn))
    {   
        show_alert("Todos os campos são obrigatórios", GTK_WINDOW(add_book_window));
        return;
    }
    
    Book *new_book = book_new(title, author, isbn);
    win->books = g_list_append(win->books, new_book); 

    GtkWidget *list_item = gtk_label_new(new_book->title);
    gtk_list_box_insert(win->books_list, list_item, -1); 
    gtk_widget_set_visible(GTK_WIDGET(win->books_list), TRUE)

}

I’m using:
GCC: 14.1.0
GTK4: 4.14.4
WIN11 - Home

Previously the warning signs were displayed with reference in the main window and the difficulty also occurred. Now my GtkAlertDialog (show_alert) references (add_book_window) and the problem persists.

I tried removing the transient_for signal and manually configuring the window closing, but the problem persists. I also added debug messages, but I couldn’t identify the reason for the main window being minimized.

I hope I can solve this problem and understand the signal passed to the main window after clicking the save button.

Question:
What could be causing the main window to minimize when closing the helper window after interacting with a GtkEntry, and how can I prevent this behavior?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.