是否可以在树视图中刷新/更新数据?

时间:2017-06-07 05:34:54

标签: c treeview refresh gtk3

我是GUI开发的新手,我已经用gtk + 3开展了一些项目,但是我已经陷入困境了。

我的应用程序包含两个树视图,行中的数据是从sql查询中获取的,通过右键单击一行弹出菜单,可以选择将数据设置为“已知”,更新数据库表。

以前的数据现在应该属于另一个树视图,所以当应用程序重新启动时它会这样做,但我希望能够实时进行。

是否可以在树视图中更新/刷新数据?

使用以下函数获得模型:

static GtkTreeStore* create_and_fill(char* known)
{
  GtkTreeStore* store;
  GtkTreeIter iter;
  GtkWidget* tree;
  store = gtk_tree_store_new(2,G_TYPE_STRING,G_TYPE_STRING);

  char source_mac[50] = {0};
  char source_ip[50] = {0};
  int num_fields;

  char query[300] = {0};
  sprintf(query, "select mac,ip from hosts where known=%s", known);
  if(mysql_query(conn, query))
  {
    fprintf(stderr,"%s\n", mysql_error(conn));
  }
  res = mysql_store_result(conn);
  num_fields = mysql_num_fields(res);

  while((row = mysql_fetch_row(res)))
  {
    sprintf(source_mac,"%s", row[0]);
    sprintf(source_ip,"%s", row[1]);
    gtk_tree_store_append(store, &iter, NULL);
    gtk_tree_store_set(store, &iter, MAC_ADDRESS, source_mac, IP_ADDRESS, source_ip,-1);
  }
  gtk_tree_store_append(store, &iter, NULL);
  mysql_free_result(res);
  return store;
}

3 个答案:

答案 0 :(得分:0)

我从未使用async function test() { for (let i = 0; i < 2; i++) { console.log('Before await for ', i); let result = await Promise.resolve(i); console.log('After await. Value is ', result); } } test().then(_ => console.log('After test() resolved')); console.log('After calling test');,只使用gtk_tree_store。然而,逻辑也适用于gtk_list_store

根据新SQL查询的结果,某些条目可能会被更改,添加或删除。

基本上有两种方法可以更新您的视图。

如果可以在表格中添加和删除行,最简单的方法可能是清理整个树并再次填充。

gtk_tree_store

如果您可以确定只有内容发生了变化,那么您只需在存储中更改它:

GtkTreeIter       iter;
gboolean          iter_valid;

iter_valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
while (iter_valid)
{
  // Maybe additional cleanup of related data
  // gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, .... -1);
  iter_valid = gtk_tree_store_remove(store, &iter);
}
// Maybe update filtering and sorting afterwards
gtk_tree_model_filter_refilter(store_filtered);

// Populate again with result of SQL query.

答案 1 :(得分:0)

我只是做了一个丑陋的补丁。 我创建了一个删除树视图的功能,重建商店并再次打包它们。

static void refresh()
{
gtk_container_remove(GTK_CONTAINER(box), tree_1);
gtk_container_remove(GTK_CONTAINER(box), tree_2);

tree_1 = create_view(create_and_fill("true")); // Rebuild the stores and views
tree_2 = create_view(create_and_fill("false"));

gtk_box_pack_start(GTK_BOX(box),tree_1,FALSE,FALSE,0);
gtk_box_pack_start(GTK_BOX(box),tree_2,FALSE,FALSE,0);

gtk_box_reorder_child(GTK_BOX(box), tree_1, x); // X being the relative position of your treeview
gtk_box_reorder_child(GTK_BOX(box), tree_2, x);
gtk_widget_show_all(box);
}

答案 2 :(得分:0)

我刚刚为点对点公告板编写了一些代码,该代码既可以填充新树,也可以向已填充树中添加新行。它使用递归函数来获取树中现有的迭代器。捕获迭代器的数组通过对此函数的引用传递。索引为0的迭代器为null,并且顶级帖子的“ inReplyTo”字段的值为0。

为简便起见,这里我仅显示相关代码。请注意,第一次调用updateTree将用$ data中的行填充裸树,然后第二次调用将$ more中的行追加。

<?php

  $model = new GtkTreeStore(GObject::TYPE_STRING, GObject::TYPE_LONG);
  $tree  = new GtkTreeView($model);
  $renderer1 = new GtkCellRendererText();
  $column1 = new GtkTreeViewColumn('Title', $renderer1, 'text', 0);
  $renderer2 = new GtkCellRendererText();
  $column2 = new GtkTreeViewColumn('ID', $renderer2, 'text', 1);
  $tree->append_column($column1);
  $tree->append_column($column2);

  function getTreeIterators($model, &$iters, $iter = null)
  {
    if ($iter === null) {
      $iter = $model->get_iter_first();
    }
    while ($iter !== null) {
      $cols       = $model->get($iter, 1);
      $id         = $cols[0];
      $iters[$id] = $iter;
      if ($model->iter_has_child($iter)) {
        $child = $model->iter_children($iter);
        getTreeIterators($model, $iters, $child);
      }
      $iter = $model->iter_next($iter);
    }
  } // getTreeIterators

  function updateTree($treeView, $data)
  {
    $model    = $treeView->get_model();
    $iters    = array();
    $iters[0] = null;
    getTreeIterators($model, $iters);
    foreach($data as $item) {
      $iters[$item->id] = $model->append($iters[$item->inReplyTo], array($item->title, $item->id));
    }
    $tree->expand_all();
  } // updateTree

  $data = array(
            (object) array('id' => 1, 'inReplyTo' => 0, 'title' => 'First post'),
            (object) array('id' => 2, 'inReplyTo' => 1, 'title' => 'Re: First post'),
            (object) array('id' => 3, 'inReplyTo' => 0, 'title' => 'Second post'),
            (object) array('id' => 4, 'inReplyTo' => 2, 'title' => 'Re: Re: First post'),
            (object) array('id' => 5, 'inReplyTo' => 3, 'title' => 'Re: Second post'),
            (object) array('id' => 6, 'inReplyTo' => 0, 'title' => 'Third post')
          );


   $more = array(
            (object) array('id' => 7, 'inReplyTo' => 0, 'title' => 'Fourth post'),
            (object) array('id' => 8, 'inReplyTo' => 1, 'title' => 'Re: That first post'),
            (object) array('id' => 9, 'inReplyTo' => 5, 'title' => 'Re: Re: Second post')
           );

  updateTree($tree, $data);
  updateTree($tree, $more);

?>