Search code examples
gtkglade

How to use Glade to prepare ListBoxRow templates that can be created and filled programatically at runtime?


I want to use a ListBox and dynamically add rows to it. I want to have several types of rows, each with its own components and layout, and at runtime, when I need to add a row, I want to take one of these row templates, create a new ListBoxRow from it, fill in the data where data needs to be filled, and add it to the ListBox.

I can do it manually in the code, but I want to use Glade to design these rows. The problems are that:

  1. Glade only lets me add ListBoxRows inside a ListBox.
  2. These rows will be part of the ListBox, not external rows I can clone.
  3. I don't see a GTK function for "cloning" a widget hierarchy, so I can't make these rows as actual widgets and then clone them.

Googling for an answer leads me to things like this, that fixate on TreeView and setting a model for it, but I need a ListBox because I need freely customizable rows.

I thought about extracting the row node into separate XML, and each time use a new builder to re-create a new row from that new XML and fill in the data. This works, but feels hacky and can't be too performent - there has to be a better way!

Is there an official way to do this?


Solution

  • Your question is actually multi-faceted, so here goes.

    1. It is possible to have a ListBoxRow outside of a Listbox by adding a row and then drag-and-dropping it outside the ListBox. This way you can add the row (to be shown later) to the Listbox using something like:

      listbox = self.builder.get_object("listbox") row = self.builder.get_object("row") listbox.insert(row, 0)

    2.It is not possible to clone or duplicate Glade widgets. See this post for more details.

    3.You might want to think about generating your widgets and ListBoxRows in code, if they happen to be fairly static. This won't work well for widgets that are getting updated in the Glade file on a regular basis.