0
votes

I am quite new with GtkTreeView. I am creating one single column with multiple rows, and I am detecting when the user is clicking on each row (or cell in this case).

I am able to retrieve the label from the cell but it is not merely enough for what I need to do. I have a struct that contains a few extra parameters that I would like to pass along. I am currently using a callback on the whole tree, as follows:

GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
g_signal_connect(selection, "changed", G_CALLBACK(on_changed), selection);

I append rows in my TreeView. Would it be possible to create a unique signal per row where I could pass my struct as the parameter please? Otherwise, is there another way to pass data for the function? The problem is that each row has a unique struct that contains different information.

Thank you very much.

EDIT:

I had not understood all the power of the TreeView. I've put everything in my model now and it works just fine. Sorry guys, I now understand the model a lot better.

1
I fixed my problem: I had not understood all the power of the TreeView. I've put everything in my model now and it works just fine. Sorry guys, I now understand the model a lot better.Jary

1 Answers

1
votes

Although you have found your answer just adding a couple of points which you might find useful:
1. When you are connecting a callback for a signal to a widget it is redundant to pass the widget to which you are connecting the callback as data. In g_signal_connect(selection, "changed", G_CALLBACK(on_changed), selection); passing selection as data is redundant as the callback function for "changed" signature has the first parameter as GtkTreeSelection * through which you will get pointer to selection. AFAIK this is true for all the signal callbacks; you will find that there is GtkWidget * (or pointer to the object for which signal callback was connected) parameter in the callback function through which will get the object for which the signal was received.
2. If you already didn't know, what you found out on your own is Model-View-Controller or MVC implementation in GtkTreeView. In this pattern, the data logic, UI & input-interaction logic are separated. GtkListStore & GtkTreeStore represent the model which hold your data. Thus to add any kind of data you make use of these. The view or the UI in this case consists of GtkTreeView, GtkCellRenderer etc. And of course, you control the interactions through handling user inputs with the aid of signals & callbacks.
Hope this helps!