开发者

How do I use gtk_list_store_set_value?

I need to store Data from text widgets in a treeview in a liststore.

To 开发者_StackOverflow中文版this end I should apparently use gtk_list_store_set_value which is capable of performing the correct typecast.

First: I don't know how. It want's a GValue but every time I try to typecast one it complains vigorously.

Second: What errors would this output if a non-numeric was input into a text field which is connected to a liststore column of type gint?

Third: Is there any easier way I seem to be missing? A reverse connection like the one automatically produced from liststore to cellrenderer?


Don't use gtk_list_store_set_value(), since there is an easier way that you are missing. First a note on "typecasting a GValue"; what are you trying to typecast from? A GValue is a container for an arbitrary data type, it's not castable to any built-in type or any GObject type. You need to construct it.

However, using gtk_list_store_set() will take care of constructing and freeing all your GValues so you don't need to worry about it. It works like this:

gtk_list_store_set(list_store, &iter,
    column_number_1, value_1,
    column_number_2, value_2,
    ...,
    -1);

So to set a single column (let's say number 0) of type gint, you would do gtk_list_store_set(list_store, &iter, 0, int_value, -1);

Don't rely on GTK to validate the input of a text field. It's much better to decide for yourself which values are valid, and check them yourself before you insert them into the list store. That will prevent any nasty surprises when someone enters a value you don't expect.


You need to create an iter, append a row to the model using gtk_list_store_append that will set the iter. Then, you need to create a GValue and set it's type and value using the functions described here. Finally call gtk_list_store_set_value passing that GValue as reference. What that looks like:

GtkListStore* model = gtk_list_store_new(columns, ...);

GtkTreeIter iter;
gtk_list_store_append(model, &iter);

for(int j = 0; j < columns; j++) {
    GValue value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_FLOAT);
    g_value_set_float(&value, 0.0);
    gtk_list_store_set_value(model, &iter, j, &value);
}

gtk_list_store_set_value was the only option I found to create columns dynamically (all of them of the same datatype). Which, BTW, if you also need that here is how I made it:

GType* types = (GType*) malloc(columns * sizeof(G_TYPE_FLOAT));
for(int i = 0; i < columns; i++) {
    types[i] = G_TYPE_FLOAT;
}

GtkListStore* model = gtk_list_store_newv(columns, types);
free(types);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜