我目前正在编写一个带有GtkTreeView
和GtkCellRendererText
且属性editable
设置为true的应用程序。当双击一个项目时,我可以对其进行编辑。现在,我希望能够添加一个空行并立即开始对其进行编辑。我尝试在将gtk_tree_view_set_cursor_on_cell
设置为true的情况下使用start_editing
。它选择该行,但不会开始编辑。我整理了一个小例子(请不要因为我只是一个小例子而没有任何错误检查)。
#include <gtk/gtk.h>
GtkListStore *store;
GtkWidget *window;
GtkWidget *view;
GtkCellRenderer *renderer;
GtkWidget *button;
GtkWidget *vbox;
void sig_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter)
{
// Set cursor on cell with start_editing = TRUE
gtk_tree_view_set_cursor_on_cell(GTK_TREE_VIEW(view), path, gtk_tree_view_get_column(GTK_TREE_VIEW(view), 0), renderer, TRUE);
}
void button_clicked(GtkButton *button, GdkEvent *event, gpointer user_data)
{
// Add empty row to liststore
GtkTreeIter iter;
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, "", -1);
}
int main (int argc, char **argv)
{
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
view = gtk_tree_view_new ();
// Create renderer and set editable to TRUE
renderer = gtk_cell_renderer_text_new ();
GValue val = G_VALUE_INIT;
g_value_init(&val, G_TYPE_BOOLEAN);
g_value_set_boolean(&val, TRUE);
g_object_set_property(G_OBJECT(renderer), "editable", &val);
g_value_unset(&val);
// Insert Text column
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
"Name",
renderer,
"text", 0,
NULL);
store = gtk_list_store_new (1, G_TYPE_STRING);
// Add test item
GtkTreeIter iter;
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
0, "Test",
-1);
gtk_tree_view_set_model (GTK_TREE_VIEW (view), GTK_TREE_MODEL(store));
vbox = gtk_vbox_new(0,0);
button = gtk_button_new_with_label("Add edit item");
gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(store), "row-inserted", G_CALLBACK(sig_inserted), NULL);
g_signal_connect(G_OBJECT(button), "button-release-event", G_CALLBACK(button_clicked), NULL);
gtk_main ();
return 0;
}
使用{p>编译它(因为我不满足所有C需求)
g++
它也可以与Gtk3一起使用,因为我使用的东西似乎没有什么改变。
有人可以指出为什么它不开始编辑吗?
答案 0 :(得分:1)
gtk_list_store_set
取消对单元格的编辑。注释掉该行,一切正常。
只需稍作修改即可找到它:
void sig_inserted(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter)
{
GtkTreeViewColumn * col = gtk_tree_view_get_column(GTK_TREE_VIEW(view), 0);
// Set cursor on cell with start_editing = TRUE
g_message ("sig_inserted", __LINE__);
gtk_tree_view_set_cursor_on_cell(GTK_TREE_VIEW(view), path, col, renderer, TRUE);
}
void button_clicked(GtkButton *button, GdkEvent *event, gpointer user_data)
{
// Add empty row to liststore
GtkTreeIter iter;
g_message ("Clicked");
gtk_list_store_append(store, &iter);
g_message ("Appended");
gtk_list_store_set(store, &iter, 0, "x", -1);
g_message ("Set");
}
void
estart (GtkCellRenderer *renderer,
GtkCellEditable *editable,
gchar *path,
gpointer user_data)
{
g_message ("renderer: start edit");
}
void
ecancel (GtkCellRenderer *renderer,
gpointer user_data)
{
g_message ("renderer: cancel edit");
}
并连接到渲染器信号:
g_signal_connect(G_OBJECT(renderer), "editing-started", G_CALLBACK(estart), NULL);
g_signal_connect(G_OBJECT(renderer), "editing-canceled", G_CALLBACK(ecancel), NULL);
给出以下输出:
** Message: Clicked
** Message: sig_inserted
** Message: renderer: start edit
** Message: Appended
** Message: renderer: cancel edit
** Message: Set