How do I set a listStore model on pre-created listbox from Glade?

馋奶兔 提交于 2019-12-25 12:49:10

问题


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'

回答1:


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.



来源:https://stackoverflow.com/questions/33470680/how-do-i-set-a-liststore-model-on-pre-created-listbox-from-glade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!