| View previous topic :: View next topic |
| Author |
Message |
beidlerj
Joined: 10 Feb 2006 Posts: 55 Location: Oregon, USA
|
Posted: Wed Feb 22, 2006 9:38 pm Post subject: Connect signals in __constructor()? |
|
|
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
|
Posted: Wed Feb 22, 2006 9:55 pm Post subject: |
|
|
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
|
Posted: Wed Feb 22, 2006 11:48 pm Post subject: |
|
|
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...  |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Thu Feb 23, 2006 12:07 am Post subject: |
|
|
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
|
Posted: Thu Feb 23, 2006 12:22 am Post subject: |
|
|
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!  |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Thu Feb 23, 2006 8:53 am Post subject: |
|
|
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 |
|
 |
|