# Can CellRendererCombo do individual rows?

**URL:** <https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353>\
**Category:** Ruby-Gnome 2\
**Created:** [February 2, 2012, 8:51am UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353 "2012-02-02T08:51:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 2, 2012, 8:51am UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/1 "2012-02-02T08:51:02Z")

</div>

Hi:

I’m trying to make CellRendererCombo work. If I understand it  
correctly,  
you set the model like this:

store = Gtk::ListStore.new)  
add rows…  
renderer.model = store

Does that mean that the same model applies to all rows?

Is it possible to make each row have its own combobox settings? For  
example, if your combobox had a list of people, then you could have a  
combobox that contains their children?

Thanks,  
Eric

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 2, 2012, 2:08pm UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/2 "2012-02-02T14:08:52Z")

</div>

Eric C. wrote in post #1043611:

> Does that mean that the same model applies to all rows?

This question makes no sense. The model contains all the rows, just like  
an array.  
Read ‘Model’, think ‘List’.

> Is it possible to make each row have its own combobox settings? For  
> example, if your combobox had a list of people, then you could have a  
> combobox that contains their children?

You misunderstand combobox I think. ComboBox is one way to display  
models. If you have one combo with parent, and one with children, then  
you have 2 models, one with parent, and one with children. And you can  
update the children model when you select a parent in the parent  
combobox.

Simon

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 2, 2012, 2:14pm UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/3 "2012-02-02T14:14:21Z")

</div>

Hi Eric,

Are you talking about having a dependant combo box? So you select a  
parent in one and it populates the 2nd with the children?

If so, I believe all you have to do is swap out the model for the  
children  
whenever a parent is selected. If the data set is going to be a  
reasonable size, you can use a hash to cache the children models so you  
don’t have to create them each time. Alternatively, instead of swapping  
out the models you can use a single one and clear / repopulate each  
time.  
Its a little slower, but with a huge dataset it saves you from wasting  
memory.

Jon

From: “Eric C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid,  
Date: 02/02/2012 02:51 AM  
Subject: [ruby-gnome2-devel-en] Can CellRendererCombo do  
individual  
rows?

Hi:

I’m trying to make CellRendererCombo work. If I understand it  
correctly,  
you set the model like this:

store = Gtk::ListStore.new)  
add rows…  
renderer.model = store

Does that mean that the same model applies to all rows?

Is it possible to make each row have its own combobox settings? For  
example, if your combobox had a list of people, then you could have a  
combobox that contains their children?

Thanks,  
Eric

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 3, 2012, 6:29am UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/4 "2012-02-03T06:29:20Z")

</div>

Sorry about my badly phrased question. Thank you for your responses.

This is what I’m trying to do exactly:

I’m trying to make a listbox for a person with these fields:

NAME  
ADDRESS  
CHILDREN (dropdown menu)

I can’t get the CHILDREN dropdown to work.

I’ve distilled it down to a short file that runs, but won’t show  
individual dropdowns for children. Just run this script, and you’ll see  
that the “row\_activated” signal dies for some reason. If you can get  
each of these rows to display their own individual menu, you will have  
fixed my problem. Notice that when you run the program, the same  
dropdow appears for every entry.

Thanks!

require ‘gtk2’

module Util # generates a random set of choices  
def Util.rand\_model  
s = Gtk::ListStore.new(String)  
r = s.append  
r[0] = rand(1000).to\_s  
r = s.append  
r[0] = rand(1000).to\_s  
r = s.append  
r[0] = rand(1000).to\_s  
return s  
end  
end

@v = Gtk::TreeView.new()  
@v.model = Gtk::ListStore.new(String)  
r = @v.model.append  
r[0] = “Hello”  
r = @v.model.append  
r[0] = “World”  
r = @v.model.append  
r[0] = “Today”

ren = Gtk::CellRendererCombo.new

# uncomment this and the value will update

# ren.signal\_connect(‘edited’) do |ren, path, text|

# i = @v.model.get\_iter(path)

# i[0] = text

# end

col = Gtk::TreeViewColumn.new

col.pack\_start( ren, false )  
col.add\_attribute( ren, :text, 0)

@v.append\_column(col)

# I tried “changed” signal too, but it doesn’t work

@v.signal\_connect(“row\_activated”) do |view, path, col|  
puts "row\_activated: "  
i = view.selection.selected  
puts i.to\_s  
rend = col.cell\_renderers[0]  
rend.text\_column = 0  
rend.model = Util.rand\_model  
end

```
ren.editable = true

```

window = Gtk::Window.new(Gtk::Window::TOPLEVEL)  
window.signal\_connect(“delete\_event”) { Gtk.main\_quit; exit! }  
window.add(@v)  
window.show\_all

Gtk.main

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 3, 2012, 10:53pm UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/5 "2012-02-03T22:53:11Z")

</div>

SOLVED!!!

Thanks for your help!!! The problem was that the row\_activated event  
was occurring after the cell was being edited. So, my program was  
trying to changed the model after the dropdown was being shown.

I changed to the “cursor\_changed” event, so now when a user  
double-clicks on a cell to edit, the model will populate on the first  
click BEFORE the edit starts.

I guess timing is everything 🙂

I’m happy to say this will be included when I release my software. Look  
for “Visual Ruby”

Thanks again,  
Eric

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 4, 2012, 10:01pm UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/6 "2012-02-04T22:01:13Z")

</div>

Eric C. wrote in post #1044009:

> I changed to the “cursor\_changed” event, so now when a user  
> double-clicks on a cell to edit, the model will populate on the first  
> click BEFORE the edit starts.

I wonder, since you change the model on the fly, what do you get when  
you read back the value after editing multiple lines ?

Simon

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 7, 2012, 10:02am UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/7 "2012-02-07T10:02:33Z")

</div>

Hi Simon:

When I said, “the model will populate on the first click…”

I was refering to the model of the dropdown menu, not the model of the  
listview itself. What happens is that the user clicks the child field,  
which fires the “cursor\_changed” event. That event populates the  
dropdown menu with the names of the children. Then, on the second  
click, the edit field (??? exact name???) event is called which switches  
the text to a combobox dropdown menu. The user can select a child from  
the list, and press . Then the “edited” event is called which  
updates the model of the listview.

So each child is edited individually. Everything works perfectly.

I’m getting very close to releasing my gem, “visualruby.” Thanks for  
your help with it!

Eric

---

<div class="post-metadata">

**Author:** ![ericrchr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8baadc/32.png) [@ericrchr](https://www.ruby-forum.com/u/ericrchr)\
**Post date:** [February 3, 2012, 3:19pm UTC](https://www.ruby-forum.com/t/can-cellrenderercombo-do-individual-rows/216353/8 "2012-02-03T15:19:56Z")

</div>

Your code almost works.. Try changing “row\_activated” to  
“row-activated”  
and double click the value to see the dropdown appear. I’m guessing you  
probably don’t want to have to double click the item to see the  
dropdown,  
so you may need to connect to a different signal.

Jon

From: “Eric C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Date: 02/03/2012 12:29 AM  
Subject: Re: [ruby-gnome2-devel-en] Can CellRendererCombo do  
individual rows?

Sorry about my badly phrased question. Thank you for your responses.

This is what I’m trying to do exactly:

I’m trying to make a listbox for a person with these fields:

NAME  
ADDRESS  
CHILDREN (dropdown menu)

I can’t get the CHILDREN dropdown to work.

I’ve distilled it down to a short file that runs, but won’t show  
individual dropdowns for children. Just run this script, and you’ll see  
that the “row\_activated” signal dies for some reason. If you can get  
each of these rows to display their own individual menu, you will have  
fixed my problem. Notice that when you run the program, the same  
dropdow appears for every entry.

Thanks!

require ‘gtk2’

module Util # generates a random set of choices  
def Util.rand\_model  
s = Gtk::ListStore.new(String)  
r = s.append  
r[0] = rand(1000).to\_s  
r = s.append  
r[0] = rand(1000).to\_s  
r = s.append  
r[0] = rand(1000).to\_s  
return s  
end  
end

@v = Gtk::TreeView.new()  
@v.model = Gtk::ListStore.new(String)  
r = @v.model.append  
r[0] = “Hello”  
r = @v.model.append  
r[0] = “World”  
r = @v.model.append  
r[0] = “Today”

ren = Gtk::CellRendererCombo.new

# uncomment this and the value will update

# ren.signal\_connect(‘edited’) do |ren, path, text|

# i = @v.model.get\_iter(path)

# i[0] = text

# end

col = Gtk::TreeViewColumn.new

col.pack\_start( ren, false )  
col.add\_attribute( ren, :text, 0)

@v.append\_column(col)

# I tried “changed” signal too, but it doesn’t work

@v.signal\_connect(“row\_activated”) do |view, path, col|  
puts "row\_activated: "  
i = view.selection.selected  
puts i.to\_s  
rend = col.cell\_renderers[0]  
rend.text\_column = 0  
rend.model = Util.rand\_model  
end

```
ren.editable = true

```

window = Gtk::Window.new(Gtk:🪟:TOPLEVEL)  
window.signal\_connect(“delete\_event”) { Gtk.main\_quit; exit! }  
window.add(@v)  
window.show\_all

Gtk.main

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
