tags:

views:

60

answers:

1

I'm trying to sort a column of data in a GTK tree view non-alphabetically. I can't seem to find a function in the GTK+ libraries that cant do such a thing.

Does anyone here know of a way to do this?

UPDATE:

Below is the code im currently trying to use:

column = gtk_tree_view_column_new();

gtk_tree_view_column_set_title(column, "Memory");

gtk_tree_view_column_set_resizable(column, TRUE);
gtk_tree_view_column_set_reorderable(column, TRUE);
gtk_tree_view_column_set_sort_indicator(column, TRUE);
/**********************************************************
gtk_tree_view_column_set_sort_column_id(column, 3);
gtk_tree_sortable_set_sort_column_id(column, 3, GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID);
**********************************************************/

gtk_tree_view_append_column(GTK_TREE_VIEW(rendered_view), column);

c_renderer = gtk_cell_renderer_text_new();

gtk_tree_view_column_pack_start(column, c_renderer, TRUE);

gtk_tree_view_column_add_attribute(column, c_renderer, "text", MEMORY);
A: 

See the GtkTreeSortable interface, which allows you to set your custom sorting function.

Notice that the commonly used standard GtkListStore model implements GtkTreeSortable.

You might also need to call gtk_tree_sortable_set_sort_column_id() to select which column to sort on. This uses the concept of "sort column id", which is not the same as column index. You need to set the proper column id on the individual GtkTreeViewColumns when you create them, using gtk_tree_view_column_set_sort_column_id().

This tutorial might be good reading, to get a better understanding of the concepts involved.

unwind
can GtkTreeSortable be used in conjunction with gtk_tree_view_solumn_set_sort_column_id?
paultop6
@paultop6: Yes, that's how you indicate which column to sort on.
unwind
@unwind: while trying to compile im getting errors that you cant convert GtkTreeViewColumn to GtkTreeSortable. I updated the question with the segment of code im having problems with. The commented out parts are to do with the question. Is there any way to cast a GtkTreeViewColumn as a GtkTreeSortable?
paultop6
@paultop6: The view column has nothing to do with GtkTreeSortable, it's an interface implemented by the *model*, i.e. the GtkListStore (or GtkTreeModel, or custom model) that you use to store your data in.
unwind
The reason i need custom sorting is because the data i need to sort has units with it as well, so i need to sort by the unit first, and then alphabetically.
paultop6
@unwind: What im struggling to understand here is where in the sequence of setting up a GtkTreeModel do i use said gtk_tree_sortable functions. I start with creating a new GtkTreeView, then i add the columns like i have done in the code above, then i save that as my model. Where in that sequence do i use the gtk_tree_sortable_sort_column_id function. And how am i going to refer it to my tree model, as the model is not an instance of a GtkTreeSortable. please note: sorry for this probably stupid questions, im still getting used to GTK.
paultop6
@paultop6: Your latest comment seems to confuse the View with the Model, I can't follow that part where you say "save that as my model". A tree model is distinct from a tree view, they are not the same. The view visualizes the content of the model. Look at the gtk_tree_sortable_set_sort_func() function, there's no problem with implementing a sorting function that compares on multiple things in sequence for grouping.
unwind
@unwind: Im confusing myself with this. I think ive thought of a simpler way to get around my problem. Is it possible, in a GtkTreeView, to have a column that isnt displayed? Would it be possible that if i clicked on the header of a visable column, it could sort the invisable column. Im thinking there is bound to be a way to do this with g_signal_connect?
paultop6
@paultop6: Just read the documentation, for a start. And yes, since you manually create the column renderers, it's totally possible to have more columns in the model than what a particular view is displaying.
unwind