在我的GTK程序中,我有一个TextEntry小部件和一个TreeView小部件。当我单击TextEntry小部件并选择一些文本,然后单击TreeView小部件时,它不会取消选择TextEntry小部件中的文本。当我单击TreeView小部件时,如何取消选择TextEntry小部件中的文本?
答案 0 :(得分:1)
您可以将gtk_editable_select_region
绑定到条目的"focus-out-event"
信号,请参见以下示例:
#include <gtk/gtk.h>
gboolean unselect_on_focus_lost (GtkWidget *widget)
{
gtk_editable_select_region (GTK_EDITABLE (widget), 0, 0);
return FALSE;
}
int main (int argc, char *argv[])
{
GtkWidget *window, *box, *entry, *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width (GTK_CONTAINER (window), 5);
gtk_window_set_default_size (GTK_WINDOW (window), 400, 130);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
gtk_container_add (GTK_CONTAINER (window), box);
entry = gtk_entry_new ();
gtk_entry_set_placeholder_text (GTK_ENTRY (entry), "I will lose my selection when you click the button below");
g_signal_connect (G_OBJECT (entry), "focus-out-event", G_CALLBACK (unselect_on_focus_lost), NULL);
gtk_container_add (GTK_CONTAINER (box), entry);
entry = gtk_entry_new ();
gtk_entry_set_placeholder_text (GTK_ENTRY (entry), "I will NOT lose my selection when you click the button below");
gtk_container_add (GTK_CONTAINER (box), entry);
/* I used a button instead of a treeview to keep the code short */
button = gtk_button_new_with_label("Click me");
gtk_container_add (GTK_CONTAINER (box), button);
gtk_widget_grab_focus (button);
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}