Re: GTK+ 2: Hide ComboBox item



Le 24/09/2013 10:17, Alessandro Francesconi a écrit :
Hello, I’m using a GTK+ 2.16 ComboBox and I wondering if there is a
way to set the visibility of a given element programmatically.

For example, having a ComboBox with 10 elements I want to hide (so
more than setting it to inactive) the 3rd element, so I need a
function like

gtk_combo_box_set_visible (mycombo, 2, false)

Does it exist?

Not like this, I'm afraid.  The only way I know to do that is to use a
GtkTreeModelFilter to filter out the elements you want to hide, e.g. by
adding a "visible" column to your model and using
gtk_tree_model_filter_set_visible_column() -- or set_visible_func() with
your function handling the logic whether or not to show a row.

Of course GtkCellRenderer has a "visible" property, but it doesn't
remove the row, it only hides the renderer itself.

Hope it helps.

Regards,
Colomban

----

/* Little example */

#include <gtk/gtk.h>

enum {
  COL_TEXT,
  COL_VISIBLE,
  N_COLS
};

int
main (int     argc,
      char  **argv)
{
  GtkWidget        *window;
  GtkWidget        *combo;
  GtkListStore     *store;
  GtkTreeModel     *filter;
  GtkCellRenderer  *renderer;

  gtk_init (&argc, &argv);

  /* data */
  store = gtk_list_store_new (N_COLS, G_TYPE_STRING, G_TYPE_BOOLEAN);
  gtk_list_store_insert_with_values (store, NULL, -1,
                                     COL_TEXT, "First row",
                                     COL_VISIBLE, TRUE,
                                     -1);
  gtk_list_store_insert_with_values (store, NULL, -1,
                                     COL_TEXT, "Second row",
                                     COL_VISIBLE, FALSE,
                                     -1);
  gtk_list_store_insert_with_values (store, NULL, -1,
                                     COL_TEXT, "Third row",
                                     COL_VISIBLE, TRUE,
                                     -1);

  /* filter */
  filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (store), NULL);
  gtk_tree_model_filter_set_visible_column (GTK_TREE_MODEL_FILTER (filter),
                                            COL_VISIBLE);

  /* combo */
  combo = gtk_combo_box_new_with_model (filter);
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
                                  "text", COL_TEXT,
                                  NULL);

  /* window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_add (GTK_CONTAINER (window), combo);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]