How to add an action to an item in the GtkListStore using Julia?

Viewed 37

I need to attach an action to the row in the list. The code below displays a window with a list, but the activation of a row (double click on it) leads to an error:

ls = GtkListStore(String, Int)
push!(ls,("Peter",20))
push!(ls,("Paul",30))
push!(ls,("Mary",25))
tv = GtkTreeView(GtkTreeModel(ls))
rTxt = GtkCellRendererText()
c1 = GtkTreeViewColumn("Name", rTxt, Dict([("text",0)]))
c2 = GtkTreeViewColumn("Age", rTxt, Dict([("text",1)]))
push!(tv, c1, c2)
signal_connect(tv_row_activated, tv, "row-activated")
win = GtkWindow(tv, "List View")
showall(win)

function tv_row_activated(w)
    println("Works")
end

enter image description here

2 Answers

As the error message says, it is expecting the callback method to be tv_row_activated(::GtkTreeViewLeaf, ::Gtk.GLib.GBoxedUnknown, GtkTreeViewColumnLeaf) i.e. the callback method should accept three arguments. Currently yours accepts a single argument w (mentioned under "Closest candidates" as tv_row_activated(::Any)).

Looking at the signature of "row-activated" in the underlying C Gtk library,

void
row_activated (
  GtkTreeView* self,
  GtkTreePath* path,
  GtkTreeViewColumn* column,
  gpointer user_data
)

it seems that the arguments passed to the callback in Julia might be the first three arguments mentioned here (though I'm not familiar enough with the library to say that for sure).

Gtk is attempting to call your tv_row_activated function with three arguments:

  1. An argument of type GtkTreeViewLeaf
  2. An argument of type Gtk.Glib.BoxedUnknown
  3. An argument of type GtkTreeViewColumnLeaf

As defined your function tv_row_activated takes a single argument, w.

Since you appear to be trying to debug or explore, I suggest redefining tv_row_activated to take any number of arguments as follows:

function tv_row_activated(w...)
    println("Works")
end

Let's test this in the REPL:

julia> function tv_row_activated(w...)
          println(w)
          println(typeof.(w))
          println("Works")
       end
tv_row_activated (generic function with 1 method)

julia> tv_row_activated(1,2,3)
(1, 2, 3)
(Int64, Int64, Int64)
Works

julia> tv_row_activated(nothing)
(nothing,)
(Nothing,)
Works

julia> tv_row_activated(5.0)
(5.0,)
(Float64,)
Works
Related