Re: Emitter reference.



On 3/7/08, Alexandre Moreira <alexandream gmail com> wrote:
> Hello, everyone.
>
>  I've been reading about Gtkmm for quite some time now, but something
>  just can't get inside my head.
>
>  I'm used to write software with Gtk+, and I grew very fond of the
>  first parameter to every signal : The Emitter pointer.
>
>  Why can't this parameter be found in any Gtkmm signal ? How do you
>  Gtkmm programmers overcome the lack of knowledge of the instance which
>  emitted the signal ?
>
>  Regards,
>
> Alexandre Moreira.

Usually we get around it by not connecting to the signals directly,
but instead subclassing the class and implementing the virtual signal
handlers such as on_clicked(), etc.  In this situation, we can know
the instance that emitted the signal: it's the object whose virtual
member function is being called (i.e. the 'this' pointer).  For
example:

class MyButton: public Gtk::Button
{
  ...
  virtual void on_clicked ()
  {
    // signal handler code. You don't need to connect anything,
    // this function will get called automatically  when the signal
    // is emmitted
  }
  ...
};

In the case where you don't subclass (but you should if possible, it
generally makes things easier), the story is slightly different.  You
have to explicitly connect to the signal, and if you want to know the
instance that emitted the signal, you unfortunately have to bind it
manually.  For instance, something like:

// handler
void clicked_cb (Gtk::Button* button)
{ ... }

// connecting to the signal
button->signal_clicked ().connect (sigc::bind (sigc::ptr_fun
(clicked_cb), button));

Hope that helps.
-- 
jonner


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]