在Gtk3开发中我在glade中使用gtkcomboboxtext小部件我选择了活动项目为零但它不起作用..所以我写了这一行
gtk_combo_box_set_active (combo,0);
在我的代码中。这可行,但它在编译时会发出一些警告,我怎样才能以干净的方式做到这一点?
显示的警告是
usr/include/gtk-3.0/gtk/gtkcombobox.h:99:15: note: expected ‘struct GtkComboBox *’ but argument is of type ‘struct GtkComboBoxText *’
test.c:247:13: warning: passing argument 1 of ‘gtk_combo_box_set_active’ from incompatible pointer type [enabled by default]
In file included from /usr/include/gtk-3.0/gtk/gtkappchooserbutton.h:29:0,
from /usr/include/gtk-3.0/gtk/gtk.h:45,
from test.c:1:
答案 0 :(得分:1)
摘自文档gtk_combo_box_new
(对于GtkBuilder创建的小部件也是如此,它们都被转换为GtkWidget,但请继续阅读):
gtk_combo_box_new ()
GtkWidget * gtk_combo_box_new (void);
Creates a new empty GtkComboBox.
Returns :
A new GtkComboBox.
Since 2.4
和另一个gtk_combo_box_set_active
gtk_combo_box_set_active ()
void gtk_combo_box_set_active (GtkComboBox *combo_box,
gint index_);
Sets the active item of combo_box to be the item at index.
combo_box :
A GtkComboBox
index_ :
An index in the model passed during construction, or -1 to have no active item
Since 2.4
问题显然是gtk_combo_box_new
的返回值是GtkComboBox
已经转换为GtkWidget
(方便),而gtk_combo_box_set_active
期望GtkCombobox
。
要沉默并修正警告,请使用投射宏GTK_COMBO_BOX (combo)
或(GtkComboBox*)combo
而不是裸combo
。
下次首先看看API文档(网络或通过devhelp)。