1

我想向 GTK+ 助手中的用户询问他的姓名,将此名称保存在全局变量中,并在不同站点的同一助手中使用此变量。我的问题是助手已完全创建并且对新站点的更改无效。

示例:我用“empty”初始化全局变量“name”。现在,助手会创建所有站点,但仅显示第一个站点。用户在“entryfield”中写下他的名字,变量“name”现在包含他的名字。助手现在显示应该包含用户名的第二个站点,但它显示字符串“empty”。

我需要用新变量“刷新”这个已经创建的条目。对不起我的英语不好,我希望我能解释一下我的问题。:)

完整的示例代码:

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

static void entry_changed    (GtkEditable*, GtkAssistant*);
static void assistant_cancel (GtkAssistant*, gpointer);
static void assistant_close  (GtkAssistant*, gpointer);

char *name = "empty";

typedef struct {
  GtkWidget *widget;
  gint index;
  const gchar *title;
  GtkAssistantPageType type;
  gboolean complete;
} PageInfo;

int main (int argc,
          char *argv[])
{
  GtkWidget *assistant, *entry, *label;
  guint i;
  PageInfo page[5] = {
    { NULL, -1, "Introduction",           GTK_ASSISTANT_PAGE_INTRO,    TRUE},
    { NULL, -1, NULL,                     GTK_ASSISTANT_PAGE_CONTENT,  FALSE},
    { NULL, -1, NULL,                     GTK_ASSISTANT_PAGE_CONTENT,  TRUE},
    { NULL, -1, "Confirmation",           GTK_ASSISTANT_PAGE_CONFIRM,  TRUE},
  };

  gtk_init (&argc, &argv);

  /* Create a new assistant widget with no pages. */
  assistant = gtk_assistant_new ();
  gtk_widget_set_size_request (assistant, 450, 300);
  gtk_window_set_title (GTK_WINDOW (assistant), "GtkAssistant Example");
  g_signal_connect (G_OBJECT (assistant), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);

  page[0].widget = gtk_label_new ("This is an example of a GtkAssistant. By\n"\
                                  "clicking the forward button, you can continue\n"\
                                  "to the next section!");
  page[1].widget = gtk_hbox_new (FALSE, 5);
  page[2].widget = gtk_entry_new();
  page[3].widget = gtk_label_new ("finish!");

  /* Create the necessary widgets for the second page. */
  label = gtk_label_new ("Your Name: ");
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (page[1].widget), label, FALSE, FALSE, 5);
  gtk_box_pack_start (GTK_BOX (page[1].widget), entry, FALSE, FALSE, 5);

  gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);

  /* Add four pages to the GtkAssistant dialog. */
  for (i = 0; i < 4; i++)
  {
    page[i].index = gtk_assistant_append_page (GTK_ASSISTANT (assistant),
                                               page[i].widget);
    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant),
                                  page[i].widget, page[i].title);
    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant),
                                  page[i].widget, page[i].type);

    /* Set the introduction and conclusion pages as complete so they can be
     * incremented or closed. */
    gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant),
                                     page[i].widget, page[i].complete);
  }

  /* Update whether pages 2 through 4 are complete based upon whether there is
   * text in the GtkEntry, the check button is active, or the progress bar
   * is completely filled. */
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (entry_changed), (gpointer) assistant);
  g_signal_connect (G_OBJECT (assistant), "cancel",
                    G_CALLBACK (assistant_cancel), NULL);
  g_signal_connect (G_OBJECT (assistant), "close",
                    G_CALLBACK (assistant_close), NULL);
  gtk_widget_show_all (assistant);
  gtk_main ();
  return 0;
}

/* If there is text in the GtkEntry, set the page as complete. Otherwise,
 * stop the user from progressing the next page. */
static void
entry_changed (GtkEditable *entry,
               GtkAssistant *assistant)
{
  const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
  gint num = gtk_assistant_get_current_page (assistant);
  GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
  gtk_assistant_set_page_complete (assistant, page, (strlen (text) > 0));

  name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}

/* If the dialog is cancelled, delete it from memory and then clean up after
 * the Assistant structure. */
static void
assistant_cancel (GtkAssistant *assistant,
                  gpointer data)
{
  gtk_widget_destroy (GTK_WIDGET (assistant));
}

/* This function is where you would apply the changes and destroy the assistant. */
static void
assistant_close (GtkAssistant *assistant,
                 gpointer data)
{
  g_print ("You would apply your changes now!\n");
  gtk_widget_destroy (GTK_WIDGET (assistant));
}
4

1 回答 1

0

在 中entry_changed,您将不得不gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);再次调用,因为您只设置了一次输入文本,这是唯一重要的事情。否则,您在设置第 3 页时设置的默认值会保留。

于 2013-09-04T12:47:05.743 回答