0

Apparently the "proper" way to have items in a GTKListBox or Treeview is constructing with a listStore model, in the constructor. What if I want to use Glade GUI project, in which a list box is already created, and referenced by builder.get_object("appsDocumentListBox")?

Can I set the model after Gtk.builder created the window, or is there a better way to do this?

I'm also wondering what the performance improvement is of using the ListStore vs manually adding with row = Gtk.ListBoxRow(), adding contents and setting ListBox.add(row)? (which does work from a Glade-builder Python window)

Unlike Treeview, apparently Listbox won't set a model after constructor?

>>> l = Gtk.ListBox()
>>> l.set_model
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ListBox' object has no attribute 'set_model'
NoBugs
  • 9,310
  • 13
  • 80
  • 146

1 Answers1

2

You are mixing two things up. You can create a GtkListBox and add GtkListBoxRows to it. There is no need for an extra model here.

There are also GtkListStore and GtkTreeStore. Both of them use a GtkTreeView. The ListStore has a flat hierarchy and the TreeStore can be nested. The GtkTreeView has a set_model function so you can set a model after you created it. You can also create the corresponding model directly in Glade.

If you have complicated widgets that you want to add, the GtkListBox is better suited. For a lot of data which is supposed to be sorted or hierarchically structured, I would rather use a GtkTreeView with a corresponding model.

elya5
  • 2,236
  • 1
  • 11
  • 27
  • How do you create a model for a listbox in Glade? Yes, I know you can add rows to listbox, but how would you handle activating a row, saving info in a structure? – NoBugs Nov 02 '15 at 15:14
  • You can add any widget to the ListBox e.g. a `GtkBox`. The ListBoxRow will be created automatically in between. To handle the activating you can connect to the `row-activated` signal and I don't understand what you mean with "saving info in a structure". – elya5 Nov 03 '15 at 12:53
  • I didn't know that, but I had added a row to a ListBox, and thought that was the wrong way to do it. On the `row-activated` signal it just passes the ListBox and the row, so there's no obvious way to get at the data you had when you created that visible title. – NoBugs Nov 03 '15 at 15:11
  • 1
    You can subclass your own ListBoxRow and provide your own method for it or do something like `get_child().get_text()` depending on your widget stacking. – elya5 Nov 05 '15 at 21:33