Re: How to align columns in a GtkTreeView widget?



Sorry for the previous badly formed post.

Again, replying to myself and cc'd to devel list because
no one replied on gtk-list.

Boszormenyi Zoltan wrote:

Hi,

I am experimenting with GTK 2.2.1 on a RedHat 9 system.

I am reading the GTK2+ API examples and have come to this problem:
I want to display int and double values in a column aligned to the right
and I want to override the default format string from "%lf" to "%.2lf".

I am doing this (following line-by-line the API doc example):

  GtkTreeIter   iter;

  items = gtk_list_store_new (N_ITEMS,
          G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_BOOLEAN, G_TYPE_INT,
          G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE);

  /* just an example line to see something */
  gtk_list_store_append (items, &iter);
  gtk_list_store_set (items, &iter,
      ITEM_BARCODE, "1234567890128",
      ITEM_NAME, "Example",
      ITEM_STORNO, FALSE,    /* not displayed column */
      ITEM_COLLECTOR, 1,
      ITEM_MULTIPLIER, 2.25,
      ITEM_PRICE, 27.0,
      ITEM_TOTAL, 2.25*27.0,
      -1);

  myview = GTK_TREE_VIEW(lookup_widget(window1, "treeview1"));
  gtk_tree_view_set_model(myview, GTK_TREE_MODEL(items));
  renderer = gtk_cell_renderer_text_new();

  column = gtk_tree_view_column_new_with_attributes(
      "Barcode", renderer,
      "text", ITEM_BARCODE,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 110);
  gtk_tree_view_column_set_max_width (column, 110);
  gtk_tree_view_column_set_fixed_width (column, 110);

  column = gtk_tree_view_column_new_with_attributes(
      "Name", renderer,
      "text", ITEM_NAME,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 160);
  gtk_tree_view_column_set_max_width (column, 160);
  gtk_tree_view_column_set_fixed_width (column, 160);

  column = gtk_tree_view_column_new_with_attributes(
      "Collector", renderer,
      "text", ITEM_COLLECTOR,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 45);
  gtk_tree_view_column_set_max_width (column, 45);
  gtk_tree_view_column_set_fixed_width (column, 45);
  gtk_tree_view_column_set_alignment (column, 1.0);

  column = gtk_tree_view_column_new_with_attributes(
      "Multiplier", renderer,
      "text", ITEM_MULTIPLIER,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 60);
  gtk_tree_view_column_set_max_width (column, 60);
  gtk_tree_view_column_set_fixed_width (column, 60);
  gtk_tree_view_column_set_alignment (column, 1.0);

  column = gtk_tree_view_column_new_with_attributes(
      "Price", renderer,
      "text", ITEM_PRICE,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 90);
  gtk_tree_view_column_set_max_width (column, 90);
  gtk_tree_view_column_set_fixed_width (column, 90);
  gtk_tree_view_column_set_alignment (column, 1.0);

  column = gtk_tree_view_column_new_with_attributes(
      "Total", renderer,
      "text", ITEM_TOTAL,
      NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW(myview), column);
  gtk_tree_view_column_set_min_width (column, 90);
  gtk_tree_view_column_set_max_width (column, 90);
  gtk_tree_view_column_set_fixed_width (column, 90);
  gtk_tree_view_column_set_alignment (column, 1.0);

What I get is the GtkTreeView widget's labels are aligned to the right
in the columns where I set but the values are not. Is it OK to use the
same GtkCellRendererText for all the columns? Or should I have to
use different renderers for differently formatted columns?


So it is compulsory to use different renderers for differently formatted columns.
because gtk_tree_view_column_new_with_attributes() is setting the attributes
of the renderer, not the column. Rereading the documentation made it clear.

Also, how to override the format string of a GtkCellRendererText displaying numeric (int, float, double) values?


This question still stands.

Best regards,
Zoltán Böszörményi





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