为什么这段代码不会将光标移动到条目的最后一个字符?
use Gtk2 '-init';
my $window = Gtk2::Window->new;
my $entry = Gtk2::Entry->new;
my $handler;
$handler = $entry->signal_connect ( 'insert-text' =>
sub {
my (
$entry,
$chrs,
$len,
$pos_strange,
$handler,
) = @_;
$entry->signal_handler_block ( $$handler );
$entry->set_text ( 'tryme' );
$entry->signal_stop_emission_by_name ('insert-text');
$entry->signal_handler_unblock ( $$handler );
$entry->set_position ( -1 );
},
\$handler
);
$window->set_size_request ( 200, 100 );
$window->add ( $entry );
$window->set_position ('center_always');
$window->show_all;
$window->signal_connect ( delete_event => sub { Gtk2->main_quit; } );
Gtk2->main;
答案 0 :(得分:3)
当最后一个insert-text
处理程序返回时,Gtk使用pos
的值更新光标位置,因此您的代码正在移动光标,然后Gtk再次移动它。您可以使用上面建议的空闲回调,但在处理程序中更新pos可能会更简洁。您希望将perl等效于*pos = g_utf8_strlen ("tryme"), 0)
而不是$entry->set_position ( -1 );