Printing input box value in dialog box in gtk c

I recently worked with gtk + in C language.

I tried to communicate between two callbacks in a dialog and not in the main window ( here ). But, unlike the solutions in the link, the program crashes (in the second callback when I try to use the widget I get from the parameters).

This is my code for this:

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>

static void print_text (GtkWidget *widget, gpointer data)
{
    GtkEntry* entry = data;
    printf("%s", gtk_entry_get_text(entry));
}

static void open_dialog (GtkWidget *widget, gpointer data)
{
    GtkWidget *window = data;
    GtkWidget *dialog;
    GtkWidget *content_area;
    GtkWidget *grid;
    GtkWidget *label;
    GtkWidget *button;
    static GtkEntry *textbox;

    dialog = gtk_dialog_new_with_buttons ("Get Text",
                                          window,
                                          GTK_DIALOG_MODAL,
                                          GTK_STOCK_OK,
                                          GTK_RESPONSE_OK,
                                          NULL);
    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    grid = gtk_grid_new();
    gtk_container_add (GTK_CONTAINER (content_area), grid);

    label = gtk_label_new("Value: ");
    gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 1, 1);
    textbox = gtk_entry_new();
    gtk_entry_set_text(textbox, "<Value>");
    gtk_grid_attach(GTK_GRID(grid), textbox, 1, 0, 1, 1);

    gtk_widget_show_all (dialog);
    g_signal_connect (GTK_DIALOG (dialog), "response", G_CALLBACK (print_text), textbox);
}

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

    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    grid = gtk_grid_new ();
    gtk_container_add (GTK_CONTAINER (window), grid);

    button = gtk_button_new_with_label ("Print Text");
    g_signal_connect (button, "clicked", G_CALLBACK (open_dialog), window);
    gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);

    gtk_widget_show_all (window);
}

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

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

    return status;
}

      

The program had to print the value of the input box, which is in the dialog box after the dialog box is closed. Note: there is a button in the main window that opens a dialog.

Thanks for the help!

+3


source to share


1 answer


The problem is the function print_text

. The prototype of the GtkDialog response signal is:

void user_function (dialog GtkDialog *, gint response_id, gpointer user_data)



So, you should change your function to:

static void print_text (GtkWidget *widget, gint response_id, gpointer data)
{
    GtkEntry* entry = data;
    printf("%s", gtk_entry_get_text(entry));
    gtk_widget_destroy (widget); // This will close the dialog
}

      

+2


source







All Articles