Gtk.ListStores and Clutter.ListModels in Javascript/gjs

It’s surprisingly hard to find this, and the generated documentation is actually misleaingly wrong1, so here’s how to create ListStores and ListModels in Javascript with gjs.

let store = new Gtk.ListStore();
store.set_column_types([GObject.TYPE_STRING, GObject.TYPE_INT]);
store.insert_with_valuesv(-1, [ 0, 1 ], [ "test", 42 ]);
let model = Clutter.ListModel.newv(
    [ GObject.TYPE_STRING, GObject.TYPE_STRING ],
    [ 'Column Name 1', 'Column Name 2' ]);
model.appendv([ 0, 1 ], [ "String 1", "String 2" ]);

The first array is the column numbers you wish to assign, the second array is the values for those columns.

Fundamental GTypes are available as GObject.TYPE_*. You can specify non-basic types using the class, e.g. Clutter.Text.

There are other variations possible, but this should provide the basics required to figure out the rest.2

  1. The n_columns parameter is a lie, and will be inferred by gjs from the array size. []
  2. You’re welcome. []

About Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She doesn't really work on GNOME any more (sadly). Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.
This entry was posted in clutter, example code, gtk, javascript. Bookmark the permalink.

One Response to Gtk.ListStores and Clutter.ListModels in Javascript/gjs

  1. ebassi says:

    in theory, this should also work:

      let model = new Clutter.ListModel();
      model.set_names([ 'Column 1', 'Column 2' ]);
      model.set_types([ GObject.TYPE_STRING, GObject.TYPE_INT ]);
      model.appendv([ 0, 1 ], [ 'Hello', 42 ]);

    but I got an error from my system gjs — which may have already been fixed in GNOME 3.4, though.

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>