cgtksignalsgtk4

GTK 4 drop down signal "activate" not working


I'm trying to setup a drop down button in my GTK application for my user to choose a day, i can create the widget, display it and connect a callback without any error, but the callback function is never called ! I wanted to use the callback function to retrived the answer the user set.

Here's the code :

// I'm on windows, using mingw64
// GTK version : 4.12.1

// callback function
void cb_test(__attribute__((unused)) GtkDropDown *self, __attribute__((unused)) gpointer data)
{
    printf("\t\t\tcall back for drop down working\n");
}

// Widget Creation
static GtkWidget *create_DT(char *question, int i, CT_Answer *answer_list_first)
{
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
    GtkWidget *drop_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
    GtkWidget *label = gtk_label_new(question);

    // create all array for the drop down

    const char *test[5] = {"test 1", "test 2", "test 3", "test 4", NULL};

    // create drop down
    GtkWidget *day = gtk_drop_down_new_from_strings(test);

    if (gtk_widget_activate(day))
        printf("the widget can be activated\n");
    
    // test if gtk 4.6 work
    gtk_drop_down_set_show_arrow(GTK_DROP_DOWN(day), FALSE);

    // set property to the widget
    gtk_box_set_homogeneous(GTK_BOX(box), true);
    gtk_box_set_homogeneous(GTK_BOX(drop_box), true);
    gtk_widget_set_hexpand_set(box, true);
    gtk_widget_set_halign(label, GTK_ALIGN_START);

    // append all widget to boxes
    gtk_box_append(GTK_BOX(drop_box), day);
    gtk_box_append(GTK_BOX(box), label);
    gtk_box_append(GTK_BOX(box), drop_box);

    // connect callback
    // g_signal_connect(day, "activate", G_CALLBACK(cb_test), NULL);
    g_signal_connect_after(day, "activate", G_CALLBACK(cb_test), NULL);

    return (box);
}

The signal "activate" should be emitted when the widget pop up its dropdown (https://docs.gtk.org/gtk4/signal.DropDown.activate.html) but the callback function isn't called when i open the dropdown with my mouse or keyboard.

But when i call the callback with g_signal_emit_by_name(day, "activate", NULL); the callback function is called : the printf display my message and the dropdown pop-up just fine.

I've also tried to create the dropdown widget with gtk_drop_down_new(model, expression); but the same issue also appeared.

Maybe i should try combo boxes, it's deprecated since 4.10...

Any advice ? Thanks in advance !


Solution

  • `// nothing selected
         gtk_drop_down_set_selected (GTK_DROP_DOWN ( day),GTK_INVALID_LIST_POSITION);
    

    ` and

     g_signal_connect_after(day, "notify::selected", G_CALLBACK(cb_test), NULL);
    

    The ::activate signal on GtkDropDown is an action signal and emitting it causes the drop down to pop up its dropdown. But it is probably about signaling when an entry has been selected. That's why the "notify" signal that DropDown inherited from GObject can be used here. This can be used to signal when a property of the object changes. The property to consider here is "selected". To prevent an entry from being preset, DropDown is still set to "No entry selected". I hope it helps you. Best regards,