0

如何检查 a 中是否已存在值GtkListStore以避免重复?我根据用户数据输入动态地从数据库中获取值,但是如果用户键入与先前键入的单词相同或相似的单词,它可以返回相同的结果,因此我的GtkListStore.

这是我当前用于将值添加到的函数GtkListStore

static inline void update_c_list(struct al_t *new_list, size_t new_list_size)
{
  struct al_t *l = new_list;
  GtkTreeIter iter;
  size_t i = 0;

  for(; i < new_list_size; i++,l++) {

    if(/* magic to avoid double goes here */) {
    gtk_list_store_append(completionmodel, &iter);
    gtk_list_store_set(completionmodel, 
               &iter, C_NAME, l->name,
               C_NICK, FOO_STRING(l->foo),
               C_EMAIL, BAA_STRING(l->baa), -1);
    }
  }

  gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(completionmodel));
}
4

1 回答 1

1

您需要迭代列表并查找其中是否存在相同的数据。

struct CListLooupStruct
{
  gbolean b_found;
  struct al_t *l;
}; 

gbolean search_c_list_func(GtkTreeModel *model, 
                           GtkTreePath *path, 
                           GtkTreeIter *iter, 
                           gpointer data)
{
   gchar name[MAX_NAME_LENGTH];
   CListLooupStruct* lookup = (CListLooupStruct*)data;
   gtk_tree_model_get (model, iter, 0, &name, -1)
   if (/*compare name to lookup->name */)
   { 
       lookup->b_found = TRUE;
       return TRUE;
   }
   return FALSE;
}

在您的update_c_list()功能中,您需要:

...
CListLooupStruct c_list_lookup = { FALSE, new_list };    
gtk_tree_model_foreach(completionmodel, search_c_list_func, &c_list_lookup);
if (/* magic to avoid double */ c_list_lookup.b_found == FALSE )
...
于 2012-11-16T03:17:51.423 回答