LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2011, 08:33 AM   #1
cvt
Member
 
Registered: Jul 2004
Location: UK
Distribution: Slackware
Posts: 37

Rep: Reputation: 17
GtkTreeView is missing the row I added.


Hey Guy's,

For some reason my treeview isn't displaying the data I just added to my liststore. I've looked at countless examples and THINK my code seems fine, but for some reason the one and ONLY one line of dummy data I've added to my liststore doesn't apear on my treeview.

Here follows the code:
Code:
int prepare_tvw_wage_list() {

GtkTreeIter itr;
GtkTreeViewColumn *col_year, *col_period, *col_gross, *col_ded, *col_net;
GtkCellRenderer *cr_0;

	tvw_wage_list = GTK_TREE_VIEW ( gtk_builder_get_object (builder, "tvw_wage_list") );
	ls_tvw_wage_list = gtk_list_store_new (5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
	
	/* Define and append columns. */
	cr_0 = gtk_cell_renderer_text_new();
	col_year = gtk_tree_view_column_new_with_attributes ("ID", cr_0, 0, NULL);
	gtk_tree_view_column_set_min_width ( col_year, 80 );
	gtk_tree_view_append_column ( tvw_wage_list, col_year );

	cr_0 = gtk_cell_renderer_text_new();
	col_period = gtk_tree_view_column_new_with_attributes ("Date", cr_0, 0, NULL);
	gtk_tree_view_column_set_min_width ( col_period, 80 );
	gtk_tree_view_append_column ( tvw_wage_list, col_period );

	cr_0 = gtk_cell_renderer_text_new();
	col_gross = gtk_tree_view_column_new_with_attributes ("Gross", cr_0, 0, NULL);
	gtk_tree_view_column_set_min_width ( col_gross, 80 );
	gtk_tree_view_append_column ( tvw_wage_list, col_gross );

	cr_0 = gtk_cell_renderer_text_new();
	col_ded = gtk_tree_view_column_new_with_attributes ("Deducts", cr_0, 0, NULL);
	gtk_tree_view_column_set_min_width ( col_ded, 80 );
	gtk_tree_view_append_column ( tvw_wage_list, col_ded );

	cr_0 = gtk_cell_renderer_text_new();
	col_net = gtk_tree_view_column_new_with_attributes ("Net", cr_0, 0, NULL);
	gtk_tree_view_column_set_min_width ( col_net, 80 );
	gtk_tree_view_append_column ( tvw_wage_list, col_net );	
	
	/* Add dummy data to my liststore. */
	gtk_list_store_append (ls_tvw_wage_list, &itr);
	gtk_list_store_set (ls_tvw_wage_list, &itr, 0, "foo", 1, "foo", 2, "foo", 3, "foo", 4, "foo", -1);

	/* Set the liststore to the treeview. */
	gtk_tree_view_set_model ( GTK_TREE_VIEW (tvw_wage_list), GTK_TREE_MODEL (ls_tvw_wage_list));	

	g_object_unref (ls_tvw_wage_list);

return 0;
}
Now, the columns come out fine, but the line of foo data that I'm adding as a test, is nowhere to be found. It worked just great for my combobox and all the examples that I've looked at does very much the same as my own code, but somewhere along the line the foo data goes awol.

I should add that I did the GUI layout in Glade. I did notice with the api docs that you can only use some of the api calls on a widget that was created using a specific api call, in this case it would have been gtk_tree_view_new_text(...), or something simmilar, but none of the calls i'm using specify this caveat, so I've dicounted that as an issue.

I've also tried re-connecting the liststore to my treeview, just on the off-chance that it made a difference.

So...

1) connect liststore to treeview
2) set liststore data
3) connect liststore to treeview

The didn't work either

Anyone spot something obvious?
 
Old 12-29-2011, 08:10 PM   #2
cvt
Member
 
Registered: Jul 2004
Location: UK
Distribution: Slackware
Posts: 37

Original Poster
Rep: Reputation: 17
Partially Solved

OK

So, the problem was that apparently the gtk+ api uses the column of the treeview widget to reserve columnar space for cellrenderers and it's the cellrenderers that display the text. So, it was quite happy with my code, and was happy to except all my data, but wouldn't, and as it turns out couldn't, display any of it. So I've now added a shared cellrender for all the columns and all my data now displays quite happily.

So, for those coming to this later, here's what works for me.
Code:
/* 
I used ONE cell renderer for ALL columns and it seems to work fine for me. 
I can't ascertain if this legal or not, but so far so good. 
I have NO doubt there will be time where this just won't do.
*/

/* Define and append columns. */
cr_0 = gtk_cell_renderer_text_new(); /* The new cell renderer */
col_year = gtk_tree_view_column_new_with_attributes ("ID", cr_0, NULL);

/* 
ARGS (the column, cellrenderer, "attribute", column to associate cell attribute with)
The list is NULL terminated.
 */
gtk_tree_view_column_set_attributes (col_year, cr_0, "text", 0, NULL); 

gtk_tree_view_column_set_min_width ( col_year, 100 );
gtk_tree_view_append_column ( tvw_wage_list, col_year );

/*
This is one column. I've obviously done that for every column, using the SAME cell render.
*/
BUT!!!!!

For it to display the values, I had to attach the text attribute of the cellrender to the columns....one at a time....I suppose I will have to do so everytime I restructure the treeview widget (as the treeviewwidget structure depends on the data passed).

I really can't understand why the treeview column widget couldn't just have a standard cellrenderer attached to it by default, where all attributes for the cell renderer would be connected to the column attributes....by default. So that you could set text, alignment etc for the treeview column and it would trickle down to the cellrenderer by default. I would have thought that in at least 80% of the time, everyone would be happy with that. If you do need to change separate attributes for the cellrenderers, you could do something like gtk_tree_view_get_cell_renderer(...), or something.

You could take it even further. Why not have this....

Code:
/* I need a new GtkTreeView widget with 5 Columns each labeled accordingly. */

gtk_tree_view_new (num_columns, ..., attributes, ....);

GtkTreeView *tree = gtk_tree_view_new_with_attributes (
   3, 
   "ID", "First Name", "Last Name", 
   "alignment", 0.5, "min-width", 80, "expand", FALSE, NULL
);
This way you could have default columns and cellrenders all connected to each other. Again, if you wanted to go further you could, just fetch a column or cellrenderer etc.

Then it came to setting the alignment on the columns. All i really would have wanted to do was the following...

Code:
gtk_tree_view_column_new_with_attributes ("ID", cr_0, "alignment", 0.5, NULL);
The reason I wanted to do that, was because that is what you would have done in glade/name-your-ide, but apparently not. This also seems to comply with the API docs which expects a float ... sorry ... gfloat. The worst thing is, I don't even know why it doesn't work, but I get a segfault somewhere all the way down in strlen(), which seemed a bit weird as we're using gfloat. So, I tried using "0.5". That seemed alright, except the change didn't show. So I was sure that must be because I needed to sync my cellrender.......erm....no.

Code:
unable to set property `alignment' of type `PangoAlignment' from value of type `gchararray'
So, I tried PANGO_ALIGN_CENTER, and I tried (gfloat)0.5 and, and , and.....
Each fails in it's own special little way.

OK.

Now, I know I've been moaning a lot here, but really, I've just been venting a little bit. I LIKE gtk+, I really do. It's a lot lighter than Qt, it's prettier than fltk (i like fltk too), it's stable and just generally GREAT. But please guy's, show me the light. Show me where I'm going wrong with this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
gnome-terminal is missing one pixel row and its an important row rednuht Linux - General 1 12-24-2009 10:30 AM
Getting Shell Script to Execute IF new row in Postgres SQL is added? (PHP) gatorpower Programming 1 03-18-2009 12:45 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
How to do Tooltips with GtkTreeView? EnigmaX Programming 1 02-16-2006 03:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration