Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Gtk2 CellRendererText: how to save the edited value in the cell?

by kikuchiyo (Monk)
on Nov 14, 2017 at 18:10 UTC ( #1203414=perlquestion: print w/replies, xml ) Need Help??
kikuchiyo has asked for the wisdom of the Perl Monks concerning the following question:

Say I have a Gtk2 application with a list that contains text columns, implemented by CellRendererTexts. There is a complete working example in Lists in Gtk2 example.

This example (and all others on the web) connect the 'edited' signal to a function that writes back the value into the model. There is one problem with this approach: the 'edited' signal does not always fire. This can be seen in the program in the linked node: when you start editing a text cell, then immediately click on one of the buttons, your modifications will be discarded - which is very annoying, especially, as in my application, the list is part of a dialog box and the button is the OK button of that dialog. I and the end user both would expect that clicking OK would acknowledge and save any modifications, and having to remember to click outside the cell but inside the list before clicking ok is extremely frustrating.

I see that Gtk2::CellRenderer can also emit an editing-changed signal, which does seem to be fired even edited is not - but the editing-changed signal does not get the row and the edited text as parameters, and I don't see where else would I get these.

So: how to do it properly?

  • Comment on Gtk2 CellRendererText: how to save the edited value in the cell?

Replies are listed 'Best First'.
Re: Gtk2 CellRendererText: how to save the edited value in the cell?
by kikuchiyo (Monk) on Nov 16, 2017 at 16:58 UTC

    I've found a solution of sorts:

    subclass Gtk2::CellRendererText, and patch its START_EDITING method to emit the 'edited' signal when the embedded entry receives a 'focus-out-event'.

    package Gtk2::CellRendererTextThatIsNotCompletelyUseless; use Glib::Object::Subclass "Gtk2::CellRendererText"; sub START_EDITING { my ($self, $event, $view, $pathstr, $back_rect, $cell_rect, $flags +) = @_; my $entry = shift->SUPER::START_EDITING(@_); $entry->signal_connect( 'focus-out-event' => sub { my ( $event_box, $event ) = @_; $self->signal_emit( edited => $pathstr, $entry->get_text() +); return 0; } ); return $entry; } 1;

    Not elegant, but it appears to work.

    I'd also like to nominate the previous reply to this thread to the 2017 Contest of Most Helpful and Well-Considered Forum Replies (and grumble that this place is not what it used to be).

      Ehh, not perfect. If the user clicks out of the cell but in the listview, the edited signal will fire twice (once normally, once thanks to the hackery above).

      Modified version:

      package Gtk2::CellRendererTextThatIsNotCompletelyUseless; use Glib::Object::Subclass "Gtk2::CellRendererText"; sub START_EDITING { my ($self, $event, $view, $pathstr, $back_rect, $cell_rect, $flags +) = @_; my $entry = shift->SUPER::START_EDITING(@_); $self->signal_handler_disconnect($self->{bad_hack}) if exists $sel +f->{bad_hack}; $self->{bad_hack} = $self->signal_connect( 'editing-canceled' => sub { my ( $event_box, $event ) = @_; $self->signal_emit( edited => $pathstr, $entry->get_text() +); return 0; } ); return $entry; }

      The appropriately named bad_hack abuse is needed to avoid connecting the signal handler again and again - but we can't just install it once and leave it be, because the anonymous sub closes over $entry, which refers to the Gtk2::Entry that is destroyed and recreated every time the user starts editing, so the need a new handler with a new closure of the newly created $entry every time.

      I feel a slight sting that there is a better way to do this, I just don't see it.

Re: Gtk2 CellRendererText: how to save the edited value in the cell?
by Anonymous Monk on Nov 15, 2017 at 02:02 UTC

    So: how to do it properly?

    Use a more signals?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1203414]
Approved by 1nickt
Front-paged by 1nickt
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-11-22 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (316 votes). Check out past polls.

    Notices?