我在Glade中创建了一个带树视图的GUI。这是XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name column1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
</object>
</child>
</object>
</interface>
我正在尝试在列表中设置一些数据并使用以下内容检索它 代码:
#include <gtk/gtk.h>
int main(int argc, char * argv[]) {
GtkBuilder* gtkBuilder = gtk_builder_new();
GtkWidget* mainwin;
gtk_init(&argc, &argv);
gtk_builder_add_from_file(gtkBuilder, "test.glade", NULL);
mainwin = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "window1"));
// set treeview data
GtkTreeView *treeview = NULL;
GtkTreeModel *model = NULL;
GtkListStore *liststore = NULL;
GtkTreeIter iter;
gchar* data = "Hello World";
gchar* item = NULL;
treeview = GTK_TREE_VIEW(gtk_builder_get_object(gtkBuilder, "treeview1"));
model = gtk_tree_view_get_model(treeview);
liststore = GTK_LIST_STORE(model);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, 0, data, -1);
gtk_tree_model_get(model, &iter, 0, item, -1);
printf("item: %s\n", item);
g_object_unref(G_OBJECT(gtkBuilder));
g_signal_connect(mainwin, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(mainwin);
gtk_main();
}
但是,数据既不会显示在树视图中,也不能检索以前插入的数据。我不知道数据是否正确插入到列表中和/或我是否只是想以错误的方式接收它。
答案 0 :(得分:2)
获取iter引用的行中一个或多个单元格的值。 变量参数列表应包含整数列号 列号后跟一个存储正在检索的值的位置。 该列表以-1结尾。 例如,从中获取值 列0的类型为G_TYPE_STRING,你可以写:gtk_tree_model_get (model,iter,0,&amp; place_string_here,-1),其中place_string_here是a gchararray要填充字符串。
因此,您应该更改代码:
gtk_tree_model_get(model, &iter, 0, item, -1);
要:
gtk_tree_model_get(model, &iter, 0, &item, -1);
另请注意:
类型为G_TYPE_OBJECT的返回值必须是未引用的, 必须释放类型为G_TYPE_STRING或G_TYPE_BOXED的值。其他 值按值传递。
因此,您需要免费item
,例如g_free (item);
,使用后。
编辑:
树视图没有TreeViewColumn,也没有CellRendererText。
我已将这些内容添加到您的glade文件中:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name column1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1">
<property name="height">40</property>
</object>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
现在应该可以了: