Treeview column cell color

Hi,

I now it’s a bad idea, to hardcode specific colors in the program.

But I would like to show a treeview with inventory for stocks. When 0 it should be the color red, when >0 green.

I’ve googled a lot, and it seems a bit tricky.

Does anyone know how to do it with the gtk_tree_store_set command, or is it not possibly?

The code i use to fill the treeview with 3 rows and 2 columns is like this:

Column 1 is Inventory_number, column 2 i number_of_item in stock.

    gtk_tree_store_append (treeStore, &iter, NULL);
    gtk_tree_store_set(treeStore, &iter, 0, "5000", -1);
    gtk_tree_store_set(treeStore, &iter, 1, "35", -1);	// should be green color

    gtk_tree_store_append (treeStore, &iter, NULL);
    gtk_tree_store_set(treeStore, &iter, 0, "5001", -1);
    gtk_tree_store_set(treeStore, &iter, 1, "0", -1);	// should be red color

    gtk_tree_store_append (treeStore, &iter, NULL);
    gtk_tree_store_set(treeStore, &iter, 0, "5002", -1);
    gtk_tree_store_set(treeStore, &iter, 1, "12", -1);	// should be green color

BR Thomas

Hi,

In your treestore, add an additional field (either of type string or Gdk.RGBA).
Bind that field with the cellrenderer’s cell-background property (or cell-background-rgba, depending on the type you choosed)
Then set the color with gtk_tree_store_set(..., "red", -1); when the value is null.

Thanks :slight_smile:

Made it work.

BR Thomas

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.