In a GtkTreeView with multiple rows, how can I programmatically update the model of a GtkCellRendererCombo on a specific line?

StackOverflow https://stackoverflow.com/questions/19323761

  •  30-06-2022
  •  | 
  •  

Question

I am writing an application that uses C/C++ and Glade 3 for the GUI.

A part of the GUI, includes a multi-line TreeView where each row represents a user. On column two there is a CellRendererCombo that represents product brands and on column three there is another CellRendererCombo that represents the products.

What I am trying to do is: whenever I set the product brand (in column 2) for a user, I want the options of the products CellRendererCombo (in column 3) to show only the products of that brand.

I tried updating the model with g_object_set but that updates all CellRendererCombo in the column.

Was it helpful?

Solution 2

In Glade:

  1. What I needed to do was add a new column of type GtkTreeModel to the model of my TreeView. Unfortunately GtkTreeModel is not part of the drop-down list that Glade provides when you create a column, so I manually typed GtkTreemodel to the type column.
  2. Then, I edited the CellRendererCombo that represents the products and defined as a model the column that I just created.

In the source code:

  1. When I load the data to the model of the TreeView, I create for each row a new ListStore and store a reference to it in the TreeView model.

    gtk_list_store_set (GTK_LIST_STORE(data->liststore_analysis), &iter, COLUMN_MODEL, GTK_LIST_STORE(data->liststore_products), -1);
    
  2. When I change the value of the CellRendererCombo that represents the brands, I update the rows in the model for the other CellRendererCombo.

    GtkListStore * list = GTK_LIST_STORE(data->liststore_products);
    GtkTreeIter iter;
    const char * openmoko[] = {"Neo 1973","Neo FreeRunner","Dash Express","3D7K","WikiReader"};
    int i, openmokoModels = sizeof (openmoko) / sizeof (*openmoko);
    for (i = 0; i < openmokoModels; i++){
        gtk_list_store_append(list, &iter);
        gtk_list_store_set(list, &iter, 0, openmoko[i], -1);
    }
    

Thank you guys for your help! :)

OTHER TIPS

As you found using g_object_set to set the model for the combobox sets the model for the whole column. What you need to do is have a column (COLUMN_COMBOBOX_MODEL) in the model you're using for the treeview which stores a reference to the model you want to use in the combobox for each row and do something like gtk_tree_view_column_add_attribute (column, combobox_renderer, "model", COLUMN_COMBOBOX_MODEL) to map the model property of the cellrenderer to the right model for each row. You can still use g_object_set to set the "text-column" and "editable" properties of the cell renderer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top