在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:

最佳答案

来自文档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的返回值是已经转换为GtkComboBoxGtkWidget(方便),而gtk_combo_box_set_active期望的是GtkCombobox

要使警告静音并修复警告,请使用强制转换宏GTK_COMBO_BOX (combo)(GtkComboBox*)combo代替裸露的combo

下次请首先查看tha API文档(通过Web或通过devhelp)。

关于linux - Gtk3 Comboboxtext小部件默认项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20369483/

10-12 19:14