Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c - Gtk.Combobox displays two columns (instead of 1), Why?

I realize GTK-2 is now 'antique'. I have an old program I have to modify, and it's just too large to convert to GTK-3 for the time I have. This is the issue:

  • I added a ComboBox to which I assign a ListBox with 2 columns (G_TYPE_INT and G_TYPE_STRING).
  • For some reason, both columns are shown on the ComboBox, though I think the code shown below only assigns one.
    void
    fill_list(GtkComboBox *cbbox)
    {
      GtkListStore *store = gtk_list_store_new(2, G_TYPE_INT, G_TYPE_STRING);
      GtkTreeIter iter;
      GtkCellRenderer *renderer = gtk_cell_renderer_text_new();


      gtk_combo_box_set_model(cbbox, GTK_TREE_MODEL(store));
      gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(cbbox), renderer, TRUE);
      gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(cbbox), renderer,
                                   "text", 0, NULL);
      gtk_combo_box_set_entry_text_column(cbbox, 0);
      gtk_list_store_append(store, &iter);
      //                               Col 0  Col 1
      gtk_list_store_set(store, &iter, 0, 0,  1, "Kind 1", -1);
      gtk_list_store_append(store, &iter);
      gtk_list_store_set(store, &iter, 0, 1,  1, "Kind 2", -1);
      gtk_list_store_append(store, &iter);

This is part of the function called on the 'realize' signal of the widget. Changing ..."text", 0... to ..."text", 1... changes the second column of the ComboBox to, as expected, the second column of the ListStore.

Capture of the (opened) Combobox, showing 2 fields

But, for unknown reason, I can't get rid of the first column. I've scanned the entire project for signs of code that might influence - no luck. The interface was generated using Glade-2.

For the last years, I've been working more from Python, so I'm suspecting there's something I'm missing here. I even basically tested this code in Python, and had no problem there.

I'd appreciate suggestions!

question from:https://stackoverflow.com/questions/65919175/gtk-combobox-displays-two-columns-instead-of-1-why

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem was solved!

I found out that Glade 2, when asked to add a GtkComboBox, actually inserted a GtkComboBoxText (even though in the Component editor it is shown as a GtkComboBox type). I've only detected this when browsing the automatically generated interface.c code.

This creates a widget with a 'model' in it, and (I suspect) the necessary code to render the single column in that model (for the 'text'). When adding a new column, by setting another model, the (original) renderer is rendering the first column.

To get rid of the original renderer, the solution seems to be to call gtk_cell_layout_clear, and only then add the new model + renderer:

gtk_cell_layout_clear(GTK_CELL_LAYOUT(cbbox));

Do this during a realize event handler for the ComboBox, don't modify interface.c, as it will be overwritten.

I'm not sure if this is enough to free any memory to the original renderer though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...