2

I'm trying to get what it's been typed in an entry and store it to afterwards use it elsewhere in my app, but I'm having a hard time trying to figure it out because functions like gtk_entry_get_text() aren't in gtk4 so I'm using an entry buffer to get content from entries.

Additionally, can somebody explain to me why there's no examples on gtk4? or why is everyone still providing examples with gkt3? Considering some functions in gkt3 are deprecated.

Here's what I'm doing:

static void on_show_text_from_buffer(GtkWidget *p_button, gpointer user_data) {
   gtk_button_set_label(GTK_BUTTON(p_button), "Clicked!");
   g_print("%s\n", (char *)user_data); // I wanna print out what it's been typed in here
}

static void on_activate(GtkApplication *app) {
   // Create a new window
   GtkWidget *window = gtk_application_window_new(app);
   GtkWidget *p_grid = gtk_grid_new();
   GtkWidget *p_button = gtk_button_new_with_label("Click me");

   gtk_grid_set_column_spacing(GTK_GRID(p_grid), 16);
   gtk_grid_set_row_spacing(GTK_GRID(p_grid), 16);

   gtk_grid_set_row_homogeneous(GTK_GRID(p_grid), TRUE);
   gtk_grid_set_column_homogeneous(GTK_GRID(p_grid), TRUE);

   GtkEntryBuffer *p_buffer_1 = gtk_entry_buffer_new(NULL, -1);

   GtkWidget *p_entry_1 = gtk_entry_new_with_buffer(p_buffer_1);
   GtkWidget *p_entry_2 = gtk_entry_new_with_buffer(p_buffer_1);

   gtk_grid_attach(GTK_GRID(p_grid), GTK_WIDGET(p_entry_1), 0, 0, 3, 1);
   gtk_grid_attach(GTK_GRID(p_grid), GTK_WIDGET(p_entry_2), 0, 1, 3, 1);
   gtk_grid_attach(GTK_GRID(p_grid), GTK_WIDGET(p_button), 0, 2, 3, 1);

   char *p_text_in_entry = g_strdup(gtk_entry_buffer_get_text(p_buffer_1));

   g_signal_connect(p_button, "clicked", G_CALLBACK(on_show_text_from_buffer),
                    p_text_in_entry);

   gtk_window_set_child(GTK_WINDOW(window), p_grid);
   gtk_window_present(gtk_window(window));
}

prxvvy
  • 109
  • 8

1 Answers1

1

GtkEntry implements GtkEditable interface. So you can do gtk_editable_get_text (GTK_EDITABLE (entry)) to get the text in a GtkEntry. Thus you don't need to set custom buffer to get the text in a GtkEntry

Mohammed Sadiq
  • 741
  • 3
  • 3