考慮下面的代碼:g_hash_table_lookup()不接受變量作爲參數
gtk_widget_show(g_hash_table_lookup(widgetbuffer,"togglebutton"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_hash_table_lookup(widgetbuffer,"togglebutton"))),TRUE);
線一條做工精細,線兩條引發此錯誤:
Gtk-CRITICAL **: gtk_toggle_button_set_active: assertion `GTK_IS_TOGGLE_BUTTON (toggle_button)' failed
這是爲什麼?如果g_hash_table_lookup正在返回gtk_widget_show處理的GtkWidget指針,那麼爲什麼不能GTK_TOGGLE_BUTTON()
強制類型轉換?
(此外,gtk_toggle_button_set_active導致對編譯沒有錯誤,只有在運行時)
編輯:爲清楚起見,是一個gtktogglebutton(注意dereferencer,它現在是如何引起的誤差):
gtk_toggle_button_set_active(*GTK_TOGGLE_BUTTON(g_hash_table_lookup(widgetbuffer,"togglebutton"))),TRUE);
note: expected ‘struct GtkToggleButton *’ but argument is of type ‘GtkToggleButton’
然而
printf("Pointer: %p\n",GTK_TOGGLE_BUTTON(g_hash_table_lookup(widgetbuffer,(gchar *) xmlGetProp(cur2, (xmlChar *) "name"))));
printf("Xml data: %s\n",xmlGetProp(cur2, (xmlChar *) "name"));
printf("hash table output pointer: %p\n\n",g_hash_table_lookup(widgetbuffer,xmlGetProp(cur2, (xmlChar *) "name")));
徹底困惑。
典型輸出:
Pointer: (nil)
Xml data: log
hash table output pointer: (nil)
只是爲了確保:
printf("Manual hash table output pointer: %p\n", g_hash_table_lookup(widgetbuffer,(gchar *) "log"));
printf("Manual hash table output pointer: %p\n", g_hash_table_lookup(widgetbuffer,"log"));
printf("Manual hash table output pointer: %p\n", g_hash_table_lookup(widgetbuffer,"(char *) log"));
所有回報:
Manual hash table output pointer: 0x1e08430
呃,它告訴你你正在投射和傳入的小部件實際上並不是切換按鈕。 – 2011-04-30 15:18:03
但是。或者更確切地說,它是一個GtkCheckButton(它繼承自togglebutton)'gtk_toggle_button_set_active'正常工作,如果我傳遞它'GTK_TOGGLE_BUTTON(gtk_builder_get_object)',而不是將其存儲在'GHashTable'中,然後傳遞它。 – 2011-04-30 16:04:57