How to use GListStore in .ui file (xml)?

I’m writing a Gtk4 application that has a GtkDropDown. I need to populate it with items of label-value pairs. And I think I need to use GListStore for the purpose.

In Gtk3, a similar class GtkListStore was available.

  <object class="GtkListStore" id="version_store">
    <columns>
      <column type="gint"/>
      <column type="gchararray"/>
    </columns>
    <data>
      <row>
        <col id="0">3</col>
        <col id="1" translatable="yes">Standard (Version 3)</col>
      </row>
      <row>
        <col id="0">5</col>
        <col id="1" translatable="yes">Updated (Version 5)</col>
      </row>
    </data>
  </object>

This class is now deprecated.

From what I read, GListStore accepts only GType in constructor. Instead of declare the column types in GtkListStore, now I should:

  1. Declare a GType (with an gint value and a gchararray for label); then
  2. Declare a GListStore with item-type set to this new GType; then
  3. Declare a GtkDropDown to use the GListStore as model.

My code is written in Python. And neither of the above steps are known to me. Especially step (1). I found previous answer of how to do this in C code. But I cannot find any example how to do this either in .ui (xml) or Python.

Please advice how I should do this. Or at least where I can find relevant documentation / manual / tutorial. Thanks a lot.

Since I’m a new user. I can only create post with 3 links.

But this is the Discourse answer I found with C code.

Hi,

As far as I know, GListStore are not buildable from *.ui files.

An easy way to do is to use a special GtkStringList, which is buildable, but you can only specify the strings:

<object class="GtkDropDown">
  <property name="model">
    <object class="GtkStringList">
      <items>
        <item>Option 1</item>
        <item>Option 2</item>
        <item>Something else</item>
      </items>
    </object>
  </property>
</object>

Then, on Python side, define some dict or list to map the string to its value.

The hard way is what you described above: create a custom type, then a custom liststore, then a custom factory to generate the strings to display. But I think it’s overkill here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.