# Question about Gtk::Builder and connect\_signals

**URL:** <https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241>\
**Category:** Ruby-Gnome 2\
**Created:** [October 27, 2011, 3:40pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241 "2011-10-27T15:40:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [October 27, 2011, 3:40pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241/1 "2011-10-27T15:40:27Z")

</div>

Looking at the source code of Gtk::Builder# **connect\_signals** , I see  
the following code:

```
  def __connect_signals__ (connector, object, signal_name,
                          handler_name, connect_object, flags)
    handler_name = canonical_handler_name(handler_name)
    if connect_object
      handler = connect_object.method(handler_name)
    else
      handler = connector.call(handler_name)
    end
  ...

```

This suggests that its possible to connect a ruby object  
“connect\_object” to a Gtk object defined in a glade file. How does one  
do this?

I was going to subclass Gtk::Builder and implement my own  
**connect\_signals** so I can do this, but maybe there is already a  
better way?

---

<div class="post-metadata">

**Author:** ![jonraphaelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jonraphaelson](https://www.ruby-forum.com/u/jonraphaelson)\
**Post date:** [October 28, 2011, 12:21pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241/2 "2011-10-28T12:21:52Z")

</div>

Jon R. wrote in post #1028799:

> Looking at the source code of Gtk::Builder# **connect\_signals** , I see  
> the following code:
> 
> ```
> def __connect_signals__ (connector, object, signal_name,
> handler_name, connect_object, flags)
> handler_name = canonical_handler_name(handler_name)
> if connect_object
> handler = connect_object.method(handler_name)
> else
> handler = connector.call(handler_name)
> end
> ...
> 
> ```
> 
> This suggests that its possible to connect a ruby object  
> “connect\_object” to a Gtk object defined in a glade file. How does one  
> do this?
> 
> I was going to subclass Gtk::Builder and implement my own  
> **connect\_signals** so I can do this, but maybe there is already a  
> better way?

I’m not sure what you expect, but maybe you can look at this discussion  
I had with Michal :  
[http://www.ruby-forum.com/topic/2488975](http://www.ruby-forum.com/topic/2488975)

We disagree, but afaik, nothing has changed, and it’s somewhat what you  
are trying to do.

regards

Simon

---

<div class="post-metadata">

**Author:** ![jonraphaelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jonraphaelson](https://www.ruby-forum.com/u/jonraphaelson)\
**Post date:** [October 28, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241/3 "2011-10-28T15:32:20Z")

</div>

Simon A. wrote in post #1028945:

> I’m not sure what you expect, but maybe you can look at this discussion  
> I had with Michal :  
> [GTK signals in Ruby - Ruby-Gnome 2 - Ruby-Forum](http://www.ruby-forum.com/topic/2488975)
> 
> We disagree, but afaik, nothing has changed, and it’s somewhat what you  
> are trying to do.

Thanks Simon. I did see that discussion, but I think I’m too new to Gtk  
programming to understand the discussion completely.

For what its worth, the way I solved my problem was to add a small layer  
to my Gtk::Builder subclass that does two things.. first I add the  
ability to attach a Ruby object to a named Gtk object from the glade  
file. Then, in the **connect\_signals** code, I look to see if an  
attached object exists for the Gtk object, if so, I automatically  
connect a method with the same name as the handler if it exists. If it  
doesn’t exist or if there is no attached object, it calls the block as  
before, but instead of only passing in the handler name, it also passes  
in the Gtk object and the signal name.

This gives me as much flexibility as possible and still be able to keep  
the code simple. I’m not sure about user data, as I’m not entirely  
clear what its used for, but maybe my solution gets around the need for  
them?

I can post my code if it helps.

---

<div class="post-metadata">

**Author:** ![jonraphaelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jonraphaelson](https://www.ruby-forum.com/u/jonraphaelson)\
**Post date:** [October 28, 2011, 4:18pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241/4 "2011-10-28T16:18:40Z")

</div>

Rather than wait, I may as well just post the code in case its something  
that others may find useful at some point. Feel free to add  
suggestions.

(note that this is a work in progress… should ignore the stuff below  
the TODO: section at the bottom)

---

<div class="post-metadata">

**Author:** ![jonraphaelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@jonraphaelson](https://www.ruby-forum.com/u/jonraphaelson)\
**Post date:** [November 6, 2011, 1:39pm UTC](https://www.ruby-forum.com/t/question-about-gtk-builder-and-connect-signals/213241/5 "2011-11-06T13:39:42Z")

</div>

Jon R. wrote in post #1028799:

> This suggests that its possible to connect a ruby object  
> “connect\_object” to a Gtk object defined in a glade file. How does one  
> do this?

I was able to answer my own question.. The idea is that in the Glade  
designer, its possible to associate another object in the window as the  
recipient of the signal. The idea is that you assign an object to the  
signal handler and the handler name should match the name of a function  
provided by the object. This allows you to handle a signal without  
writing any code.

This means that we are still left with no way in Ruby-GNOME2 to assign a  
signal handler based on the object and signal its associated with. This  
is easily accomplished by passing these as parameters to the  
connect\_signals block.

Since adding these to the connect\_signals block doesn’t affect existing  
implementations, I’d like to propose that we add these to the  
Ruby-GNOME2 project.
