linux - Gtk3 Comboboxtext 小部件默认项

标签 linux ubuntu gtk gtk3

在 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 的返回值是已经转换为 GtkWidgetGtkComboBox(方便),而 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/

相关文章:

c++ - 尝试转换到 GTK3 时未在此范围内声明 GTK_OBJECT(针对 gtk+-3.0 库进行编译)

c - GTK+ 覆盖 gtk_radio_button 或 gtk_toggle_button 的默认处理程序

c++ - gtkmm 和 gstreamermm 测试代码抛出运行时错误

ruby-on-rails - 使用 rails 3.1.0 和 ubuntu 安装 Nokogiri 1.5.0 时出错

c - 如果 dlopen() 失败,dlerror() 如何返回 NULL?

c - setsockopt() 错误 : Numerical argument out of domain

linux - AWK、grep 和 Find 命令之间有何不同,因为所有这三个命令都在文本文件或目录中查找特定的短语?

linux - 通过另一个用户建立隧道 ssh

linux - 漂亮的 shell 脚本(菜单、图像、抗锯齿、1080p、颜色、淡入淡出效果等)?

c++ - g++ 编译错误