Question

I'm trying to make a small program to manipilate data between GtkEntry fields. To do this I'm using global variables. But using g_print functions to show me the contents I get NULL data! What is wrong in my way?

#include <gtk/gtk.h>

const gchar *entry0_text;
const gchar *entry1_text;
const gchar *entry2_text;

static void my_function (GtkWidget *widget, GtkWidget *entry)
{
  g_print ("Entry0 contents: %s\n", gtk_entry_get_text (GTK_ENTRY (entry)));

  g_print ("Entry1 contents: %s\n", *entry0_text);
  g_print ("Entry2 contents: %s\n", *entry1_text);

}

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

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), ":-)");
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
  gtk_container_set_border_width (GTK_CONTAINER (window), 20);

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

  entry0 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry0, 0, 0, 1, 1);
  entry0_text = gtk_entry_get_text (GTK_ENTRY (entry0));

  entry1 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry1, 1, 0, 1, 1);
  entry1_text = gtk_entry_get_text (GTK_ENTRY (entry1));

  entry2 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry2, 0, 1, 1, 1);
  entry2_text = gtk_entry_get_text (GTK_ENTRY (entry2));

  button = gtk_button_new ();
  gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 1, 1);
  g_signal_connect (button, "clicked", G_CALLBACK (my_function), entry0);

  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;
}

I'm using Debian GNU/Linux and compiling with a script like this: gcc pkg-config --libs --cflags gtk+-3.0 -o $2 $1

Thank You!

Was it helpful?

Solution

The values for entry0_text - entry2.text are retrieved during the startup of your program. But since there has been no entry made yet, they contain empty strings.

You have to use gtk_entry_get_text for all of your entry widgets inside the callback function my_function, not inside the activate function.

Besides that, you are using the second and third g_print incorrectly; you should use

g_print ("Entry1 contents: %s\n", entry1_text);
g_print ("Entry2 contents: %s\n", entry2_text);

Remember that after your program has started, entry1_text and entry2_text contain empty strings. But with *entry1_text and *entry2_text you won't receive the empty string, but the first element of this string, which is the terminating '\0' null character, this is why you receive a 'null' for these g_prints. So use entry1_text instead of *entry1_text.

The following code is working properly; I've used the entry widgets and strings as global variables for simplicity.

#include <gtk/gtk.h>
#include <string.h>

GtkWidget *entry0;
GtkWidget *entry1;
GtkWidget *entry2;

const gchar *entry0_text;
const gchar *entry1_text;
const gchar *entry2_text;

static void my_function (void)
{
  entry0_text = gtk_entry_get_text (GTK_ENTRY (entry0));
  entry1_text = gtk_entry_get_text (GTK_ENTRY (entry1));
  entry2_text = gtk_entry_get_text (GTK_ENTRY (entry2));

  g_print ("Entry0 contents: %s\n", entry0_text);
  g_print ("Entry1 contents: %s\n", entry1_text);
  g_print ("Entry2 contents: %s\n", entry2_text);

}

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

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), ":-)");
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);
  gtk_container_set_border_width (GTK_CONTAINER (window), 20);

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

  entry0 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry0, 0, 0, 1, 1);

  entry1 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry1, 1, 0, 1, 1);

  entry2 = gtk_entry_new ();
  gtk_grid_attach (GTK_GRID (grid), entry2, 0, 1, 1, 1);

  button = gtk_button_new ();
  gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 1, 1);
  g_signal_connect (button, "clicked", G_CALLBACK (my_function), NULL);

  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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top