Vala confused me about construction functions!

Hi

        var search_entry = new Gtk.SearchEntry ();
        search_entry.set_name("GridEntry");
        var sfilter = new Gtk.StringFilter (new Gtk.PropertyExpression (typeof (ItemPoets), null, "name"));
        var filter = Gtk.Filter (sfilter);
        filter.set_match_mode (Gtk.StringFilterMatchMode.PREFIX);
        filter.set_ignore_case(true);
        var filter_model = new Gtk.FilterListModel (single_selection, filter);
        filter_model.set_incremental(true);
        //filter.set_search ("test");
        filter.changed(Gtk.FilterChange.DIFFERENT);
        search_entry.bind_property ("text", filter, "search", 0);

if I write: var filter = Gtk.Filter (sfilter);

error: use `new’ operator to create new objects

if I write: var filter = new Gtk.Filter (sfilter);

error: Access to non-public constructor `Gtk.Filter.new’

Does anyone guide me how to do this?

Gtk.StringFilter is a subclass of Gtk.Filter so you don’t need it at all here.

If the function requires a Gtk.Filter, you should just cast the Gtk.StringFilter to one.

In general i don’t think you are expected to use Gtk.Filter directly unless you are subclassing it.

1 Like

Gtk.Filter does not have a public constructor: you can only subclass it, or instantiate one of its subclasses:

  • Gtk.BoolFilter
  • Gtk.CustomFilter
  • Gtk.FileFilter
  • Gtk.MultiFilter
  • Gtk.StringFilter

A Gtk.StringFilter is already a Gtk.Filter, so you don’t create a Gtk.Filter instance wrapping a Gtk.StringFilter: you cast a Gtk.StringFilter to Gtk.Filter. See the tutorial for “dynamic type casting”.

2 Likes

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