getting arguments with g_signal_connect GTK4

69 Views Asked by At

I am doing an exercise in which i have a button, an entry, a box and a label. the button has to call a funcion that creates a label and gives it the entry input. finally the funcion has to add the label to the box and show the label in the window. how can i give the label pointer, the box pointer and the entry pointer to the callback function without using any global pointer?

#include <gtk/gtk.h>

static void print(GtkApplication *app, gpointer user_data)
{
}

static void activate(GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *button;
    GtkWidget *entry;
    

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Window");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
    gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
    gtk_window_set_child(GTK_WINDOW(window), box);

    label = gtk_label_new("il bottone sotto non fa nulla");
    gtk_box_append(GTK_BOX(box), label);

    button = gtk_button_new_with_label("Button 1");
    g_signal_connect(button, "clicked", G_CALLBACK(print), NULL);
    gtk_box_append(GTK_BOX(box), button);

    entry = gtk_entry_new();
    gtk_box_append(GTK_BOX(box), entry);



    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

1

There are 1 best solutions below

0
Holger On

It's a little tricky, but it works.

You hand over the box to the callback function. Then you determine the child GtkEntry. You read the contents and create a new label with this that you add to the box. Have fun practicing.

#include<gtk/gtk.h>

static void print(GtkApplication *app, gpointer user_data)
{
    GtkWidget *child;
    for(child = gtk_widget_get_first_child(GTK_WIDGET(user_data));
            child != NULL;
            child = gtk_widget_get_next_sibling(child))
    {
        if (strcmp(gtk_widget_get_name(child),"GtkEntry") == 0)
        {
            GtkWidget *label_2 = gtk_label_new(gtk_editable_get_text(GTK_EDITABLE(child)));
                gtk_box_append(GTK_BOX(user_data),label_2);
        }
    }
}

static void activate(GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *button;
    GtkWidget *entry;


    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Window");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
    gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
    gtk_window_set_child(GTK_WINDOW(window), box);

    label = gtk_label_new("il bottone sotto non fa nulla");
    gtk_box_append(GTK_BOX(box), label);

    button = gtk_button_new_with_label("Button 1");
    g_signal_connect(button, "clicked", G_CALLBACK(print), box);
    gtk_box_append(GTK_BOX(box), button);

    entry = gtk_entry_new();
    gtk_box_append(GTK_BOX(box), entry);



    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}

enter image description here