Gtk4 GtkFlowBox bind_model data not expected type

Hello,

In my application, I want to populate a FlowBox from a database using a model. Using GtkFlowBox#bind_model, the data passed to my widget creation function is not the item I retrieved from the database and put into my model, but instead the class trying to create the widget, even though the documentation makes it seem like it should be the object from the model.

Am I doing something wrong?

CategoriesView.vala:

public Categories() {
    base("categories", "Categories");

    this.db = Database.@get();
    this.model = db.get_categories_model();

    try {
        this.db.rebuild_categories();
        this.flow_box.bind_model(model, (Gtk.FlowBoxCreateWidgetFunc) create_new_button);
    } catch (IOError e) {
        critical("Error building categories: %s", e.message);
    }
}

private Widgets.CategoryButton create_new_button(Object data) { // I expect data to be my Category class
    debug("type: %s", data.get_type().name()); // prints "RecipeBookViewCategories"
    return null;
    //  var button = new Widgets.CategoryButton(data);
    //  button.clicked.connect(() => {
    //      this.button_clicked(button.category.id);
    //  });
    //  return button;
}

Category.vala:

namespace RecipeBook {
    /**
     * Represents a category of recipes.
     */
    public class Category : Object {
        /** The `id` of this category. */
        public string id { public get; construct; }
        /** The `name` of this category. */
        public string name { public get; construct; }
        /** The `description` of this category. */
        public string description { public get; construct; }
        /** The `image_path` of this category. */
        public string? image_path { public get; construct; }

        public Category(string id, string name, string description, string? image_path) {
            Object(
                id: id,
                name: name,
                description: description,
                image_path: image_path
            );
        }
    }
}

Resolved this thanks to the help of a member of the Vala Discord server. The fix was to change the function signature to…

private Gtk.Widget create_new_button(Object data) {
    //
}

…and dropping the cast to FlowBoxCreateWidgetFunc when passing it to bind_model. For some reason, casting it causes this to be passed to the widget create function instead of the model data object.

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