在 Gtk3 开发中,我在林间空地使用 gtkcomboboxtext 小部件,我选择事件项目为零,但它不起作用。所以我写了这一行
gtk_combo_box_set_active(组合,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:
最佳答案
摘自文档 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
的返回值是已经转换为 GtkWidget
的 GtkComboBox
(方便),而 gtk_combo_box_set_active
需要一个 GtkCombobox
。
要消除并修复警告,请使用转换宏 GTK_COMBO_BOX (combo)
或 (GtkComboBox*)combo
而不是裸露的 combo
。
下次首先查看 API 文档(网络或通过 devhelp)。
关于linux - Gtk3 Comboboxtext 小部件默认项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20369483/