gpt4 book ai didi

c - 从 GtkListStore 检索数据

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:57 25 4
gpt4 key购买 nike

我在 Glade 中创建了一个带有 TreeView 的 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();
}

但是,数据既没有显示在 TreeView 中,也无法检索之前插入的数据。我不知道数据是否已正确插入到列表中和/或我是否只是试图以错误的方式接收数据。

最佳答案

来自gtk_tree_model_get ()文档:

Gets the value of one or more cells in the row referenced by iter . The variable argument list should contain integer column numbers, each column number followed by a place to store the value being retrieved. The list is terminated by a -1. For example, to get a value from column 0 with type G_TYPE_STRING, you would write: gtk_tree_model_get (model, iter, 0, &place_string_here, -1), where place_string_here is a gchararray to be filled with the string.

因此,您应该更改代码:

gtk_tree_model_get(model, &iter, 0, item, -1);

收件人:

gtk_tree_model_get(model, &iter, 0, &item, -1);

另请注意:

Returned values with type G_TYPE_OBJECT have to be unreferenced, values with type G_TYPE_STRING or G_TYPE_BOXED have to be freed. Other values are passed by value.

因此,您需要释放 item,例如g_free (item);,使用后。

编辑:

TreeView 没有 TreeViewColumn,也没有 CellRendererText。

我已将这些添加到您的空地文件中:

<?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>

它现在应该可以工作了:

enter image description here

关于c - 从 GtkListStore 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com