我在Glade中创建了一个具有树状视图的图形用户界面这是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();
}

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

最佳答案

gtk_tree_model_get ()文档中:
获取iter引用的行中一个或多个单元格的值。
变量参数列表应包含整数列编号,每个
列号,后跟存储要检索的值的位置。
列表以-1结尾例如,要从
第0列使用G_type_STRING类型,您将编写:gtk_tree_model_get
(model,iter,0,&place_string_here,-1),其中place_string_here是
gchararray将填充字符串。
因此,您应该更改代码:

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

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

另请注意:
必须未引用具有G_type_对象的返回值,
必须释放类型为G_type_STRING或G_type_BOXED的值其他
值是按值传递的。
因此,您需要在使用后释放item,例如g_free (item);
编辑:
treeview没有treeview列,也没有CellRenderText。
我已经把它们添加到你的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>

现在应该可以了:
c - 从GtkListStore检索数据-LMLPHP

10-05 18:07
查看更多