Re: C++-classes.



On 18 Oct 2002 09:35:06 +0100
James Durie <jdurie anvil co uk> wrote:

On Fri, 2002-10-18 at 09:23, David Larsson wrote:
Hi.

How do you get this to work.

/* Class Definition */
class GtkClass
{
  public:
    void GtkClass();
    void Test();
  private:
    GtkWidget *button;
};

GtkClass::GtkClass()
{
  button=gtk_button_new();

  /* The part that doesn't work. */
  gtk_signal_connect(GTK_OBJECT(button), "clicked",
  GTK_SIGNAL_FUNC(Test), NULL);
}

How is the Method Test to be declared, and how do you call it from a
gtk_signal_connect()?

Thanks

// David

You can't pass a method in as a callback function pointer. There is a
hidden parameter, this, to all methods which points to the instance of
the class.

Instead try:
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(clicked_cb), this);


I would like to say that I never used GTK with C++ before but does not following
work? Or something like this:

gtk_signal_connect(GTK_OBJECT(button), "clicked",
        GTK_SIGNAL_FUNC(this->Test), NULL);



where clicked_cb is:
void clicked_cb(GtkButton *button, GtkClass *c)
{
      c->Test();
}


That is the simplest way to get a callback calling a method and how I
have done it in my own projects.

James


-- 
James Durie               Phone:  +44 20 7749 7904
Anvil Software Limited    Fax:    +44 20 7749 7916
46-48 Rivington Street    e-mail: jdurie anvil co uk
London EC2A 3QP

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


      Peeter Vois
http://my.tele2.ee/vois



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