难以编辑的GtkTreeView [英] Difficulty in editable GtkTreeView

查看:175
本文介绍了难以编辑的GtkTreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个软件,我的用户应该可以在该软件中将数据添加到类似表格的编辑小部件中,我设法使用GtkTreeView进行呈现.通过此调用设置其editable属性,我能够将单元格变为可编辑状态

I am writing a piece of software where my user should be able to add data to a table-like editing widget, which I managed to render by using a GtkTreeView. I was able to render my cell editable by setting its editable property via this call

g_object_set(content_renderer,
        "editable", TRUE,
        NULL);

但是,我的GtkTreeView不仅不保留输入的值,因为它甚至不显示渲染前添加的数据.我在网上看到了一些示例,其中开发人员手动将用户输入数据设置为模型,但是所有这些都是使用提供的这些语言的绑定用Python或C ++编写的,因此无法直接解决我的问题.

However, my GtkTreeView not only doesn't retain values entered as it's not even showing the data I've added before rendering. I saw a few examples in the web where the developer manually set the user input data to the model but all of these were either written in Python or C++ using offered bindings for these languages and therefore don't address my issue directly.

我已经写了这个(不是那样的)小例子,该问题已成功显示.

I've written this (not so) small example where the problem is successfully shown.

如何使用户输入数据持久保存在GtkTreeView中?

How can I make user input data persistent in a GtkTreeView?

PS:我的问题在某种程度上与

P.S.: my problem is somehow related to this one, however this solution doesn't apply to me.

编辑: 我遵循了@PhillipWood提示,并将GtkCellRendererText连接到edited信号,还手动将新数据设置到了模型中.

EDIT: I followed @PhillipWood hint and connected my GtkCellRendererText to the edited signal, and also set by hand the new data into the model.

但是,我在版本之前输入的数据和我在版本期间输入的数据都不会显示在网格中.

HOWEVER, neither the data I've entered prior to the edition nor the data I've entered during the edition appear in the grid.

我在Fedora 19下使用GTK + 3.8.8.

I am under Fedora 19, with GTK+ 3.8.8.

推荐答案

您需要连接到单元格渲染器的已编辑"信号.当用户完成编辑时,将发出此消息,这取决于应用程序(即您的代码)将新值存储在模型的正确列中.

You need to connect to the 'edited' signal of the cell renderer. This is emitted when the user finishes editing, it is up to the application (i.e. your code) to store the new value in the correct column of the model.

更新:

看看更新后的代码,有几件事很引人注目.

Looking at your updated code there are a few things that stand out.

首先,当您使用GtkListStoreGtkTreeStore时,最好创建一个用于为列编制索引的枚举.

Firstly when you use a GtkListStore or a GtkTreeStore it is a good idea to create an enum for indexing the columns.

enum {COLUMN_LABEL, COLUMN_CONTENT, COLUMN_LAST};

然后在创建列表存储区时执行

Then when you create the list store do

list_store = gtk_list_store_new(COLUMN_LAST, G_TYPE_STRING, G_TYPE_INT);

创建树形列时,需要告诉它要与cellrenderer一起显示模型的哪些列.您可以通过将cellrenderer的属性绑定到模型中的列来完成此操作

When you create a tree column you need to tell it which column(s) of the model to display with the cellrenderer. You do this by binding properties of the cellrenderer to columns in the model

label_col = gtk_tree_view_column_new_with_attributes ("Layer",
                                                      gtk_cell_renderer_text_new(),
                                                      "text", COLUMN_LABEL, 
                                                      NULL);

现在,模型中的内容列将存储一个int,因此我们不能仅绑定渲染器的text属性,因为它需要一个字符串.我们需要使用

Now the content column the model stores an int so we cannot just bind the text property of the renderer as it expects a string. We need to map the column contents onto the text property using

content_column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_cell_data_func (content_column,
                                         gtk_cell_renderer_text_new (),
                                         content_column_data_func,
                                         NULL, NULL);

使用

static void
content_column_data_func (GtkTreeViewColumn *tree_column,
                         GtkCellRenderer *cell,
                         GtkTreeModel *tree_model,
                         GtkTreeIter *iter,
                         gpointer data)
{
  int value;
  gchar text;

  gtk_tree_model_get (tree_model, iter, COLUMN_CONTENT, &value, -1);
  text = g_strdup_printf ("%d", value);
  g_object_set (cell, "text", text);
  g_free (text);
}

最后,在编辑的回调中,需要先将字符串转换为整数,然后再存储

Finally in the edited callback you need to convert the string to an integer before you store it

int value = atoi (new_text);
gtk_list_store_set (list_store, &iter, COLUMN_CONTENT, value, -1);

这篇关于难以编辑的GtkTreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆