GTK+2.0 Combo Box - 如何获取条目的位置?

GTK+2.0 Combo Box - How to get the position of an entry?

#include <gtk/gtk.h>
//Defined a comboBox like this 
combo = gtk_combo_box_new_text();

//Added entries like this 
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "string");

//How to get a particular entry deleted from the list of entries in the ComboBox combo?

如何删除组合框中的特定条目?例如 - ComboBox 下拉列表有 "Foo"、"Boo"、"Hoo" 等条目。现在我想 select "Boo" 然后删除它?我该怎么做?

//Segmentation fault while removal error

mcve -

fixed = gtk_fixed_new();
combo = gtk_combo_box_new_text();
gtk_fixed_put(GTK_FIXED(fixed), combo, 50, 50);
label = gtk_label_new("Rooms");
gtk_fixed_put(GTK_FIXED(fixed), label, 50, 30 );
gtk_table_attach_defaults (GTK_TABLE (table), fixed, 2, 3, 0, 2);
g_signal_connect(G_OBJECT(combo), "changed", G_CALLBACK(combo_selected), (gpointer) label);

在这个回调函数中,我只是将当前 selection 复制到全局声明的字符串中。接下来,在其他一些函数中,我尝试 运行 this -

gtk_combo_box_remove_text (GTK_COMBO_BOX(combo), gtk_combo_box_get_active (GTK_COMBO_BOX(combo))); //statement where seg fault occurs

gtk_combo_box_get_active 为我提供了要删除的适当索引条目。注意 - GtkWidget *combo 在程序中全局定义。

可以通过gtk_combo_box_get_active and it can be removed with help of gtk_combo_box_text_remove or gtk_combo_box_remove_text获取组合框中活动(当前选中)条目的id (取决于使用哪个版本的api。前者(gtk_combo_box_text_remove)是最新的)。

gtk_combo_box_get_active returns -1 if there is no active item and should therefore be checked before passing it to one of the remove functions. This is especially important if, as in the case presented above, the removing happens in a signal handler. According to the documentation "changed" 信号

[...] is emitted when the active item is changed. This can be due to the user selecting a different item from the list, or due to a call to gtk_combo_box_set_active_iter(). It will also be emitted while typing into the entry of a combo box with an entry. [...]

据我了解,这包括取消选择项目的时间(如果删除项目,这可能会发生)。

因此,您的处理程序应该看起来像这样:

void combo_selected(GtkComboBox *widget, gpointer user_data)
{
    int active_id=gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
    if(active_id!=-1)
    {
        //do real work...
    }
}