# How do I reconstruct columns dynamically on GtkTreeView

**URL:** <https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779>\
**Category:** Ruby-Gnome 2\
**Created:** [March 25, 2010, 11:42pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779 "2010-03-25T23:42:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [March 25, 2010, 11:42pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/1 "2010-03-25T23:42:46Z")

</div>

Any idea? thanks in advance

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [March 26, 2010, 9:07am UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/2 "2010-03-26T09:07:49Z")

</div>

2010/3/25 Javier M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Any idea? thanks in advance

Could you be more precise ? I don’t get what you want to do.

Simon

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [March 26, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/3 "2010-03-26T17:55:27Z")

</div>

Sorry. My intention is to show the data from tables of database, that  
the user can select. Therefore, a priori, I don’t know either the number  
or type of the columns that I must show. The Question is how I can  
dynamically create the ListStore.new, Now better?  
Very grateful

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [April 8, 2010, 11:41am UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/4 "2010-04-08T11:41:28Z")

</div>

2010/3/26 Javier M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Sorry. My intention is to show the data from tables of database, that  
> the user can select. Therefore, a priori, I don’t know either the number  
> or type of the columns that I must show. The Question is how I can  
> dynamically create the ListStore.new, Now better?

Considering the pseudo table

_User_  
id integer  
name string  
birth date

If you can get the type of columns, try to make a switch.

model\_types = Array.new  
table\_attributes\_type.each do |attribute\_type|  
case attribute\_type  
when ‘tinyint’, ‘integer’, ‘longint’  
model\_types \<\< Numeric  
when ‘varchar’, ‘text’  
model\_types \<\< String  
[…]  
end  
my\_list\_store = ListStore.new(model\_types)

If you can’t have the types, you might put everything as string, and  
do the conversion on the fly.  
Calling ‘to\_s’ when filling the model  
Calling ‘to\_i’, ‘to\_f’, Date.parse, etc when reading from the model.

so the model would :  
my\_list\_store = ListStore.new([String] \* number\_of \_attributes)

Makes sense ?

Simon

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [April 8, 2010, 12:58pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/5 "2010-04-08T12:58:45Z")

</div>

Hei Simon,

you can find my approach here →

> <https://github.com/bolthar/freightrain/blob/master/lib/extensions/gtk/widgets/gtk_treeview.rb>

In short: i store the reference to the object in the first column, every  
other field i want to display will be a string, so the ListStore.new  
call looks something like:

ListStore.new([Object, [String] \* no\_of\_fields\_to\_display].flatten)

On callbacks i do every operation on the object, the fields are just for  
display. I don’t know if this is suitable to your case.

Bye!

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.

> **[using (IMHO)](https://usingimho.wordpress.com)**
>
> Andrea Dallera 's blog

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [April 8, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/6 "2010-04-08T13:33:18Z")

</div>

2010/4/8 Andrea D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On callbacks i do every operation on the object, the fields are just for  
> display. I don’t know if this is suitable to your case.

It’s not my case, I’m just answering Javier :).

Thanks anyway.

I will have to take a look at freightrain, I’m mostly doing everything  
by hand right now.  
It would be nice to have a higher level lib, MVC style.  
I don’t have much time though, unfortunately.

Simon

---

<div class="post-metadata">

**Author:** ![ayanami](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ayanami](https://www.ruby-forum.com/u/ayanami)\
**Post date:** [April 8, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/how-do-i-reconstruct-columns-dynamically-on-gtktreeview/185779/7 "2010-04-08T13:49:07Z")

</div>

Oh, I am sorry 🙂 I am very new to the mailing list, i guess the  
original message was posted before i joined.

There’s no documentation for freightrain at the moment so I am aware  
that it’s kind of difficult to understand how to use it. I am working on  
that though, i hope i can put up a website with all needed to learn it  
in a month or so. Thanks for the interest anyway!

Bye!

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.

> **[using (IMHO)](https://usingimho.wordpress.com)**
>
> Andrea Dallera 's blog
