我正在尝试制作一个小程序来处理GtkEntry字段之间的数据。为此,我使用全局变量。但是使用g_print函数向我显示我得到NULL数据的内容!我怎么了?
#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: %sn", gtk_entry_get_text (GTK_ENTRY (entry)));
g_print ("Entry1 contents: %sn", *entry0_text);
g_print ("Entry2 contents: %sn", *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;
}
我正在使用Debian GNU/Linux,并使用这样的脚本进行编译:gcc pkg-config --libs --cflags gtk+-3.0
-o$2$1
谢谢!
entry0_text-entry2.text的值在程序启动期间检索。但是由于还没有输入,所以它们包含空字符串。
您必须在回调函数my_function
中为所有入口小部件使用gtk_entry_get_text
,而不是在activate
函数中。
除此之外,您还错误地使用了第二个和第三个g_print;你应该使用
g_print ("Entry1 contents: %sn", entry1_text);
g_print ("Entry2 contents: %sn", entry2_text);
请记住,程序启动后,entry1_text和entry2_text包含空字符串。但是使用*entry1_text和*entry2_text,您不会收到空字符串,而是收到该字符串的第一个元素,即终止的"\0"null字符,这就是为什么您会收到这些g_print的"null"。因此,请使用entry1_text而不是*entry1_text。
以下代码工作正常;为了简单起见,我使用了入口小部件和字符串作为全局变量。
#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: %sn", entry0_text);
g_print ("Entry1 contents: %sn", entry1_text);
g_print ("Entry2 contents: %sn", 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;
}