# Moving rows included on a gtk treeview using a single button

**URL:** <https://www.ruby-forum.com/t/moving-rows-included-on-a-gtk-treeview-using-a-single-button/105138>\
**Category:** Ruby-Gnome 2\
**Created:** [May 26, 2007, 4:00am UTC](https://www.ruby-forum.com/t/moving-rows-included-on-a-gtk-treeview-using-a-single-button/105138 "2007-05-26T04:00:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/u/Julio_CSSSSsar_Sosa)\
**Post date:** [May 26, 2007, 4:00am UTC](https://www.ruby-forum.com/t/moving-rows-included-on-a-gtk-treeview-using-a-single-button/105138/1 "2007-05-26T04:00:14Z")

</div>

Hello everybody i’m new here :), I’m trying to get handle of gtk  
treeviews,  
right now, i know how to create rows, columns and some else, but i have  
a  
problem, i want to move the rows using a button, come on the classic up  
and  
down, well i don’t an idea to do that =/ i’m learning about ruby + gtk,  
i  
think this combination is simply wow, here is the code i’m using glade,  
libglade can someone let me a little of light?, some example to do that  
or  
just talk me something about, using glade or simple gtk.

how you can see, i’m following some of your examples.  
watch the picture too, to get a better idea.

I extend my regards to everyone greetings.

Julio CÃ©sar Sosa Yeladaqui.

±----------------------  
+treeview.rb  
±----------------------

#!/usr/bin/env ruby

=begin  
treeview.rb - Ruby/Libglade2 sample script.

Copyright © 2002-2006 Ruby-GNOME2 Project Team  
This program is licenced under the same licence as Ruby-GNOME2.

$Id: treeview.rb,v 1.5 2006/06/17 14:14:55 mutoh Exp $  
=end

# Manejo y control de Listas o treeview de gtk mediante glade en Ruby

# Crear ---------- Si

# Ordenar -------- Si

# Eliminar ------- Pendiente

# Mover Arriba/Abajo -------Pendiente

require ‘libglade2’

class Treeview  
def initialize(path)  
@glade = GladeXML.new(path) {|handler| method(handler)}  
@treeview = @glade[“treeview1”]  
@model = Gtk::TreeStore.new(String, String)

```
crt = Gtk::CellRendererText.new

```

# column = Gtk::TreeViewColumn.new(“Test”, Gtk::CellRendererText.new,

{:text =\> 0})

# column2 = Gtk::TreeViewColumn.new(“hola”,

Gtk::CellRendererText.new,  
{:text =\> 1})

```
column = Gtk::TreeViewColumn.new("Test", crt, {:text => 0})
column2 = Gtk::TreeViewColumn.new("hola", crt, {:text => 1})

```

#-----------------------------------------

```
root_iter = @model.append(nil)

rootx_iter = @model.append(nil)

```

#-------------------------------------  
@treeview.append\_column(column)

```
root_iter.set_value(0, "hello guys")

rootx_iter.set_value(0, "moco")

```

# child1 = @model.append(root\_iter)

# child1.set\_value(0, “Child1”)

# @treeview.set\_model(@model)

#-------------------------------------  
@treeview.append\_column(column2)

# root\_iter = @model.append(nil)

```
root_iter.set_value(1, "jojojo")

rootx_iter.set_value(1, "loco")

```

# hijo\_col2 = @model.append(root\_iter)

# hijo\_col2.set\_value(1, “yo no jui”)

#--------------------------------------  
@treeview.set\_model(@model)

```
@treeview.set_size_request(200, 200)

```

end  
end

Treeview.new(“treeview.glade”)  
Gtk.main

---

<div class="post-metadata">

**Author:** ![Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/u/Julio_CSSSSsar_Sosa)\
**Post date:** [June 8, 2007, 9:51am UTC](https://www.ruby-forum.com/t/moving-rows-included-on-a-gtk-treeview-using-a-single-button/105138/2 "2007-06-08T09:51:38Z")

</div>

Julio César Sosa Yeladaqui wrote:

> Hello everybody i’m new here :), I’m trying to get handle of gtk treeviews,  
> right now, i know how to create rows, columns and some else, but i have a  
> problem, i want to move the rows using a button, come on the classic up and  
> down, well i don’t an idea to do that =/ i’m learning about ruby + gtk, i  
> think this combination is simply wow, here is the code i’m using glade,  
> libglade can someone let me a little of light?, some example to do that or  
> just talk me something about, using glade or simple gtk.

Try putting something like this in Treeview.initialize:

```
down_button = @glade["button4"]
down_button.signal_connect("clicked"){
  selected_item = @treeview.selection.selected
  @model.move_before( selected_item, nil ) # move to end of list
}

```

This will cause the block of code, between { and }, to be executed  
when the down button is clicked. This code will move the  
selected row to the bottom of the list. This isn’t exactly what  
you want. You’ll have to read the documentation for Gtk::TreeStore  
to find out how to do it. Also this code does not do any error  
checking, it crashes if no line is selected when you press the down  
button. Something else that can be easily fixed.

I hope this helps. And welcome to ruby/gtk!

Martin

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@Julio\_CSSSSsar\_Sosa](https://www.ruby-forum.com/u/Julio_CSSSSsar_Sosa)\
**Post date:** [June 9, 2007, 5:36am UTC](https://www.ruby-forum.com/t/moving-rows-included-on-a-gtk-treeview-using-a-single-button/105138/3 "2007-06-09T05:36:03Z")

</div>

thanks a lot Martin i will try it n\_\_\_n i love this community

2007/6/8, Martin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Try putting something like this in Treeview.initialize:  
> you want. You’ll have to read the documentation for Gtk::TreeStore  
> This [SF.net](http://SF.net) email is sponsored by DB2 Express  
> Download DB2 Express C - the FREE version of DB2 express and take  
> control of your XML. No limits. Just data. Click to get it now.  
> [http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
