2016-03-07 64 views
0

我只是試圖更改文本內容窗口小部件中的文本內容窗口小部件中的輸入。我使用Glade來設計這個簡單的界面,在窗口中只包含這兩個小部件。然後將其轉換爲GIO資源,並將其加載到程序中glib-compile-resources test.xml設置GTK +條目或標籤的文本不起作用

但標籤不變。任何想法我做錯了什麼?編譯器不會給出任何警告。我可以打印標籤的內容,也是我從Glade設置的內容。

test.c的

#include <gtk/gtk.h> 

static void on_entry_activate(GtkWidget *entry, gpointer data) { 
    const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry)); 
    GtkBuilder *app_builder = gtk_builder_new_from_resource(
           "/org/samik/test/test.glade"); 
    GObject *label = gtk_builder_get_object(app_builder, "label1"); 
    g_print("%s\n", gtk_label_get_text(GTK_LABEL(label))); 
    gtk_label_set_text(GTK_LABEL(label), text); 
} 
static void on_app_activate(GApplication *app, gpointer data) { 
    GError *res_load_err = NULL; 
    GResource *app_res = g_resource_load("test.gresource", &res_load_err); 
    g_resources_register(app_res); 
    GtkBuilder *app_builder = gtk_builder_new_from_resource(
           "/org/samik/test/test.glade"); 
    GObject *entry = gtk_builder_get_object(app_builder, "entry1"); 
    GObject *main_window = gtk_builder_get_object(app_builder, "window1"); 
    g_signal_connect(GTK_WIDGET(entry), "activate", 
     G_CALLBACK(on_entry_activate), NULL); 
    gtk_window_set_application(GTK_WINDOW(main_window), GTK_APPLICATION(app)); 
    gtk_widget_show_all(GTK_WIDGET(main_window)); 
} 
int main(int argc, char *argv[]) { 
    int status; 

    GtkApplication *app = gtk_application_new("org.samik.draw", 
          G_APPLICATION_FLAGS_NONE); 
    g_signal_connect(app, "activate", G_CALLBACK(on_app_activate), NULL); 
    status = g_application_run(G_APPLICATION(app), argc, argv); 

    g_object_unref(app); 

    return status; 
} 

test.glade

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Generated with glade 3.16.1 --> 
<interface> 
    <requires lib="gtk+" version="3.10"/> 
    <object class="GtkWindow" id="window1"> 
    <property name="can_focus">False</property> 
    <property name="title" translatable="yes">test</property> 
    <child> 
     <object class="GtkFixed" id="fixed1"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <child> 
     <!-- take whatever user types in this text-entry --> 
      <object class="GtkEntry" id="entry1"> 
      <property name="width_request">100</property> 
      <property name="height_request">80</property> 
      <property name="visible">True</property> 
      <property name="can_focus">True</property> 
      </object> 
      <packing> 
      <property name="x">197</property> 
      <property name="y">119</property> 
      </packing> 
     </child> 
     <child> 
      <!-- and put it here --> 
      <object class="GtkLabel" id="label1"> 
      <property name="width_request">100</property> 
      <property name="height_request">80</property> 
      <property name="visible">True</property> 
      <property name="can_focus">False</property> 
      <property name="label" translatable="yes">label</property> 
      </object> 
      <packing> 
      <property name="x">53</property> 
      <property name="y">56</property> 
      </packing> 
     </child> 
     </object> 
    </child> 
    </object> 
</interface> 

的test.xml

<?xml version="1.0" encoding="UTF-8"?> 
<gresources> 
    <gresource prefix="/org/samik/test"> 
     <file compressed="true">test.glade</file> 
    </gresource> 
</gresources> 

回答

0

當你調用gtk_builder_new_from_resource()兩次,你基本上創建兩個窗口(但只顯示一個)。修改標籤的調用是正確的,但它會修改不可見窗口中的標籤。

只在初始化時加載資源一次,併爲指向實際小部件的指針提供回調函數。通常,您會創建一個指向後面需要的所有小部件的指針的結構,然後將指向該結構的指針作爲userdata傳遞給像on_entry_activate()這樣的回調函數。

+0

啊,現貨!你有沒有在Devhelp手冊中找到這個?換句話說,這個功能是否記錄在案? – Samik