mikeppalmer
mikeppalmer

Reputation: 41

GtkListStore - how to center text?

I have included a brief snippet of code. I'm making a Tree View with 5 columns. I have pasted the last column with the for loop that generates the data to be stored in the GtkListStore. I can center the columns of the Tree View easily. I have looked online to find out how to center the text in the GtkListStore and I am not finding a solution. I have looked through the documentation:

http://developer.gnome.org/gtk3/3.4/GtkListStore.html

On the last link, I do not see an alignment property. Is there a way to align all of the objects through the GtkTreeModel? I haven't found any online examples using the GtkListStore and aligning text... really appreciate the help!

// Append Table Velocity column
column = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(column, "Pressure");
gtk_tree_view_column_set_min_width(column, 60);
gtk_tree_view_column_set_alignment(column, 0.5); // 0.0 left, 0.5 center, 1.0 right
// Code Above center's the column title in the Tree View

renderer = gtk_cell_renderer_text_new();
g_object_set( G_OBJECT( renderer ), "xalign", 0.5 );    // xalign, 0.5
// Code above doesn't change alignment..

gtk_tree_view_column_pack_start(column, renderer, FALSE);
gtk_tree_view_column_set_attributes(column, renderer, "text", 4, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

// List Store
liststore = gtk_list_store_new(5, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore));

int i;
for(i=1; i<=6; i++) {
// Append test data
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, 0, i, 1, "5", 2, "0.2", 3, "123", 4, "0.5", -1);
// How do I center the text stored in the GtkListStore?
}

gtk_widget_show_all(window);

Upvotes: 0

Views: 3029

Answers (3)

angelo dureghello
angelo dureghello

Reputation: 11

A gtk4 help/update can be useful now a day.

GtkColumnView and GListStore should be used over all the legacy stuff.

Inside the setup_cb, add gtk_widget_set_halign(label, GTK_ALIGN_START);

Upvotes: 0

mikeppalmer
mikeppalmer

Reputation: 41

I was able to find a convenient function called gtk_tree_view_column_with_attributes(). Using this function and two more lines of code I can conveniently center the text in the header and the list store.

// Append Pressure column
column = gtk_tree_view_column_new_with_attributes("Pressure", renderer, "text", 4, NULL);
gtk_tree_view_column_set_alignment(column, 0.5);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);

Upvotes: 2

GTK 1.2.6 fanboy
GTK 1.2.6 fanboy

Reputation: 879

Try g_object_set (renderer, "xalign", 0.5, NULL); with the NULL at the end, g_object_set needs a sentinel. Actually leaving out the sentinel should have given you at least a compiler warning, or did you leave it out deliberately or by mistake?

Upvotes: 1

Related Questions