日期:2014-05-16  浏览次数:20725 次

GTK怎样根据值找到列表控件中指定的一行?
GtkListStore可以插入一行一行的数据,可是我现在想根据一行的第一列数据的值找到这一行数据。找到它的指针。

比如有一下一个列表
名字  分数
chao  60
xiaom 100
defei  90
我知道了xiaom这个名字想在列表中找它的分数,怎样获得指向xiaom 100的iter才能读取它的分数值。
我知道了xiaom这个名字,我想把它从列表中删除怎么做呢?
------解决方案--------------------
/* 把名字和分数组合成结构体,方便你用指针操作 */
typedef struct
{
gchar *name;
gint   score;
}
Student;

gint i = 0;
GtkListStore *model;
GtkTreeIter iter;

array = g_array_sized_new (FALSE, FALSE, sizeof (Student), 1);

/* create list store */
model = gtk_list_store_new (NUM_ITEM_COLUMNS, G_TYPE_INT, G_TYPE_STRING,
                              G_TYPE_INT, G_TYPE_BOOLEAN);

/* 想通过名字获取分数的话,你需要得到array和i,当然,你已经知道名字了 */
for (i = 0; i < array->len; i++)
{
gtk_list_store_append (model, &iter);
/* 这里是设置,获取的原理是一样的 */
gtk_list_store_set (model, &iter,
  g_array_index (array, Student, i).name,
  g_array_index (array, Student, i).score,
  -1);
}

/* 
* 关于删除也一样,需要知道iter,GTK只有两种删除,
* 一个是删除全部,另一个是删除一行
*/

/* iter要和tree配合,我这里没写 */
if (gtk_tree_selection_get_selected (selection, NULL, &iter)){
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
}

临时写的,不全之处,楼主自行补齐
------解决方案--------------------
并非是遍历,下面两个GTK的函数,第一个删除一行,第二个删除全部
/* Removes the given row from the list store. After being removed, iter is set to be the next valid row, or invalidated if it pointed to the last row in list_store */
gboolean gtk_list_store_remove(GtkListStore *list_store, GtkTreeIter *iter);

/* Removes all rows from the list store */
void gtk_list_store_clear(GtkListStore *list_store);

------解决方案--------------------
我是用鼠标选择的,如果你要遍历当然不是不可以