 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
Peter GTK+ Geek
Joined: 10 May 2008 Posts: 54
|
Posted: Sat May 10, 2008 2:16 pm Post subject: Begginner C/GTK treeview/listview question |
|
|
Dear GTK Gurus,
I am just starting at GTK on Linux (I have been doing Windows programming...)
and I am running into some problem. I have a treeview widget in my main window and a button.
Upon creation I fill the treeview with value using the below code. It puts the
SigName character strings in the list.
Now my problem is that when I call LISTALL_Tree() from the button callback
again (when SigName strings are different) instead of displaint it in a single column it
adds as a new column to the existing list.
All I want is to refresh the list with the new value s.
Can anyone help, please:
Thanks.
Peter
v | Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| oid LISTALL_Tree(GtkWidget *window) {
GtkTreeIter iter, itersel;
GtkWidget *treeview, *scrolled_win;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkTreeModel *model;
int i, j, R, Ndata, click = 0, n;
char edtext[80];
treeview = lookup_widget (window, "Signaltree");
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
Ndata = sizeof(SigNames)/sizeof(SigNames[0]);
store = gtk_list_store_new (1, G_TYPE_STRING);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Signal", renderer, "text",
SIGNAME_COLUMN, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
// fill the tree
for (i = 0; i < Ndata; i++) {
/* Add a new row to the model */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, SIGNAME_COLUMN, SigNames[i], -1);
} // i
treeview = lookup_widget (window, "Signaltree");
gtk_tree_view_set_model(GTK_TREE_VIEW(treeview),
GTK_TREE_MODEL(store));
g_object_unref(store);
gtk_widget_show(treeview);
}
// the button calback function:
on_ListAllbutton_clicked (GtkButton *button,
gpointer user_data)
{
char edtext[80];
static int clicks = 0;
GtkWidget *InfoLabel, *treeview;
GtkTreeModel *model;
treeview = lookup_widget (Mainwindow, "Signaltree");
model = gtk_tree_view_get_model(treeview);
store = GTK_LIST_STORE ( model );
gtk_list_store_clear(store);
LISTALL_Tree(Mainwindow);
} | |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 497 Location: Portland, OR USA
|
Posted: Sat May 10, 2008 4:06 pm Post subject: |
|
|
You only need to create the model and append columns once, typically when the application is first started. To repopulate, you'll only need to clear the model and then run through that little loop that puts data into the model:
| Code: (C) | 1 2 3 4 5
| for (i = 0; i < Ndata; i++) {
/* Add a new row to the model */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, SIGNAME_COLUMN, SigNames[i], -1);
} | |
|
| Back to top |
|
 |
Peter GTK+ Geek
Joined: 10 May 2008 Posts: 54
|
Posted: Sat May 10, 2008 9:31 pm Post subject: |
|
|
| Micah Carrick wrote: | You only need to create the model and append columns once, typically when the application is first started. To repopulate, you'll only need to clear the model and then run through that little loop that puts data into the model:
| Code: (C) | 1 2 3 4 5
| for (i = 0; i < Ndata; i++) {
/* Add a new row to the model */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, SIGNAME_COLUMN, SigNames[i], -1);
} | |
Micah, thank you for the fast reply.
Queston about model:
Should I keep the model as global? Or in the callback recreate before the
little loop to populate the list:
treeview = lookup_widget (Mainwindow, "Signaltree");
model = gtk_tree_view_get_model(treeview);
store = GTK_LIST_STORE ( model );
for (....)...etc
Or set model as static in the Treesetupo function and call
Treesetup from the callback (bypassing the tree creation part) ?
Peter |
|
| Back to top |
|
 |
Peter GTK+ Geek
Joined: 10 May 2008 Posts: 54
|
Posted: Sat May 10, 2008 10:50 pm Post subject: |
|
|
| Micah Carrick wrote: | You only need to create the model and append columns once, typically when the application is first started. To repopulate, you'll only need to clear the model and then run through that little loop that puts data into the model:
| Code: (C) | 1 2 3 4 5
| for (i = 0; i < Ndata; i++) {
/* Add a new row to the model */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, SIGNAME_COLUMN, SigNames[i], -1);
} | |
Deam Micah,
Still no go...
My button callback looks like this (see below);
When I start up the treview is created and shown with all list items in OK.
But when I press the button. only one item is shown in it and when I move the mouse pointer over the treeview list soem items pop up\out of the nowhere... strange.
What's going on?
Peter
PS: the compiler is complaining about the argument type of gtk_label_set_text(InfoLabel, edtext); What is the proper cast?
Here is is button press callback:
l:
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void
on_LISTALLbutton_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget *InfoLabel;
char edtext[80];
static int clicks = 0, i, R;
GtkWidget *treeview;
GtkTreeModel *model;
GtkTreeIter iter;
GtkListStore *store;
treeview = lookup_widget (Mainwindow, "Signaltree");
model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
store = GTK_LIST_STORE ( model );
gtk_list_store_clear (store);
for (i = 0; i < 7; i++) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, SIGNAME_COLUMN, SigNames[i], -1);
}
InfoLabel = lookup_widget (Mainwindow, "Infolabel");
clicks++;
sprintf(edtext, "LISTALL:number of clicks:%d", clicks);
gtk_label_set_text(InfoLabel, edtext);
g_object_unref(store);
gtk_widget_show(treeview);
}
| [/code] |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|