How to filter a GLib.ListStore

Hi,

I’m updating a tiny app (here) from Gtk 3 to Gtk 4.

And starting with Gtk 4.10, Gtk.ListStore, Gtk.TreeView & co are deprecated, so I used a Gtk.ColumnView, with a Gtk.FilterListModel, which use a GLib.ListStore model.

My GLib.ListStore contains a list of really simple Object.

What I want to do :
When a user is using the Gtk.SearchEntry, I simply want to compare his input with two members of my object (ip_address, hostname) and filter the list accordingly.

But I don’t know how to proceed :
For the filter model of Gtk.FilterListModel, I use a Gtk.StringFilter but I have no clue on how to use that.

My understanding so far is that the Gtk.StringFilter :

  • should use one of the sub-classes of Gtk.Expression which need to be evaluated in some way, to filter items list (to be setup with the method set_expression (Expression? expression) ;
  • and to “activate” the filtering process, I need to use the filter method set_search (string? search), which should use the evaluation result of the Gtk.Expression ?

My problem is that I can’t find any relevant example of usage of Gtk.Expression on the net so I don’t understand how I can use that. All examples about filtering a ListStore are with the Gtk.ListStore :confused:

Note I use a resource file main-window.ui to setup the different models, factories & co.
I suspect I need to setup all the filtering stuff in the construct method and use the method set_search (string? search) in the method which handle the changed signal of the Gtk.SearchEntry.

To summarize, the question is : how to filter a GLib.ListStore. If Gtk.Expression is the way to do it, how can I use it ? If it’s not the way, can you pin-point me in the right direction - please ? :slight_smile:

Thanks for reading this far.
Any help is welcome …

Ok, in the gtk-demo app, there’s the Settings example which do exactly what I want, with a Gtk.ListView and Gtk.ColumnView & co.
I’ll update this thread when I have something working, to have a reference in Vala.

1 Like

Ok, I have something that seems to works (code here ) even if I’m not totally sure it’s the right way…

UI resource file structure

Gtk.ColumView
|  `-> model: Gtk.NoSelection
|       `-> model: Gtk.FilterListModel
|            |-> model: GLib.ListStore
|            `-> filter: Gtk.StringFilter
|--> Gtk.ColumnViewColum 1
|    `-> factory: Gtk.SignalListItemFactory
|--> Gtk.ColumnViewColum 2
|    `-> factory: Gtk.SignalListItemFactory
|--> Gtk.ColumnViewColum 3
     `-> factory: Gtk.SignalListItemFactory

Code

  1. For each Gtk.ColumnViewColumn we have to handle their factory signals - sent each time there are items appended or removed in the store:
    • setup - to create the widget displayed in the column. (appended to GLib.ListItem.child ) ;
    • bind - to setup the widget behaviour depending some values of an item that we can get with GLib.ListItem.item ;
    • unbind - to remove the widget when an item is removed from the store.
  2. To filter our store model with a property value of an item, we can use a Gtk.PropertyExpression:
    PropertyExpression property_expression = new PropertyExpression (typeof(YourObjetItem), null, "property_name_of_object_item");
    
  3. Assign this expression to the Gtk.StringFilter with his method set_expression (Expression property_expression)
  4. In the callback method which handle, for instance, a user input, we have to use Gtk.StringFilter.set_search (string user_input) method.

And that’s all.

1 Like

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