gnope php gtk gui gnope.org  

Connect signals in __constructor()?

 
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners
View previous topic :: View next topic  
Author Message
beidlerj



Joined: 10 Feb 2006
Posts: 55
Location: Oregon, USA

PostPosted: Wed Feb 22, 2006 9:38 pm    Post subject: Connect signals in __constructor()? Reply with quote

Hello again -

Is it not possible to connect signals within a constructor function? If not, why? I have a class which extends GtkDialog. I set up stock Cancel and OK buttons (and response IDs) when calling the parent constructor, then use GtkDialog::add_button() to add a couple custom buttons alongside the stock buttons in the action area. Everything works fine until I try to connect the "clicked" signal for my two custom buttons to a private method of my class. The reason I set things up this way is so that clicking "OK" or "Cancel" will end the dialog, at which point the response ID is sent back to the caller, etc., but the two custom buttons (Select All and Clear All) will perform operations on the contents of the dialog without ending it.

I have tried every variation I can think of for connect() or connect_simple() such as:

Code:
$allButton->connect("clicked", array("self", "selectAll");
or
$allButton->connect_simple("clicked", array("$this", "selectAll");
etc...

Am I doing something wrong?
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Wed Feb 22, 2006 9:55 pm    Post subject: Reply with quote

private methods can only be accessed from the class itself.

The callback is called from the Gtk main loop, which is outside the class scope. Thus it can't access the private methods you defined. Only public methods can be used as callback.
Back to top
beidlerj



Joined: 10 Feb 2006
Posts: 55
Location: Oregon, USA

PostPosted: Wed Feb 22, 2006 11:48 pm    Post subject: Reply with quote

Okay, now PHP is happy with the callback, at least as far as calling it goes. Very nice.

Now I'm confused about objects a little... in the callback function, I reference $this, since the button that triggered the callback is a member of my class. I thought I should be able to get at the members of the class, but I get a
Code:
PHP Fatal error:  Using $this when not in object context...
Is that also because the program is in the gtk_main loop when the callback gets triggered? How, then, do I get the callback, which is defined inside the class definition, to be able to access the members of its class instance?

I hope that made sense... Confused
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Thu Feb 23, 2006 12:07 am    Post subject: Reply with quote

You need to use
Code:
connect_simple('signalname', array($this, 'methodName'))

See that I don't use quotes around $this.
Back to top
beidlerj



Joined: 10 Feb 2006
Posts: 55
Location: Oregon, USA

PostPosted: Thu Feb 23, 2006 12:22 am    Post subject: Reply with quote

Beauty!

I had been using quotes around $this because all the examples I had come across were something like
Code:
connect_simple('destroy', array('gtk', 'main_quit'));


You're the best, man! Very Happy
Back to top
cweiske



Joined: 08 Dec 2005
Posts: 454
Location: Leipzig/Germany

PostPosted: Thu Feb 23, 2006 8:53 am    Post subject: Reply with quote

You're mixing two things here:

- Callbacks can call to instance methods of objects. You use then
Code:
array($obj, 'methodName')

- Or you want to call static methods on classes, like the gtk::main_quit(). Then you use
Code:
array('classname', 'methodName')
Back to top
Display posts from previous:   
Post new topic   Reply to topic    gnope.org Forum Index -> PHP GTK2 Beginners All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Templates enhanced by DigiWiki. Hosting by Tradebit.