How to add new item to gtk4 version 4.8.3 ColumnView the builder way?

,

Hello folks, I’m a gnome newbie, working on a C, in gtk4 version 4.8.3. to create an application that displays multiple columns. I am learning how to add an item through the action button, but it is not working… I only found one example, but I found it very complex to “listview_settings, on gtkdemo”. I tried very things, but not work. So I got stuck and ask for help.

My button action:



static void
run_add_item_cb(GtkButton * mbuttton, gpointer *mbuilder)
{
    GtkBuilder *builder;

    builder = gtk_builder_new_from_file("../../list/gtkaddtolist.xml");

    GListStore *store = G_LIST_STORE(gtk_builder_get_object(builder, "glist_store_model_id"));

    BookData *mbook_data = book_data_new("Mrs",
                                         "Eustace Sheehy",
                                         "Mystery",
                                         "1/18/1921",
                                         "563887571-9",
                                         "Yuan Renminbi",
                                         "2.4",
                                         "Bluezoom",
                                         "Tswana",
                                         "270");

    // Append the new item to the list store
    g_list_store_append(store, mbook_data);

    // Unref the item, as the list store now holds a reference
    g_object_unref(builder);
    g_object_unref(mbook_data); // Unref the new_person as the store now owns a reference

    g_print("\nRun Add Item...!!!");
}

Any help, more information or example will be appreciate it…

Hi,

that doesn’t sound right… that will create a new GListStore, while you probably want to reuse the existing one.

Thank you to reply… :+1:
I will discovery how to recovery that one that i am using. Maybe this is the problem why is not working.

1 Like

Side note: GTK 4.8.3 was released in December 2022. The current stable release of GTK 4 is 4.18.6. I recommend you try and use a more recent version than one released two and a half years ago. There have been multiple bug fixes in GtkColumnView alone, for instance.

1 Like

Thanks for the tip. :+1: I am trying to use this version because it is the same as my operating system. Debian 12.

Debian is going to release a new stable version soon, so you’ll be hit by nearly 3 years of development in one update.

My strong recommendation is to use Flatpak for applications; at the very least, you’ll be able to control the dependencies a lot better than with Debian stable.

2 Likes

Thank you again. :+1:
If things don’t get up with this version I’m using, I really think about it.

Gemini helped me with some examples to fix my problem…

Thanks everyone for the messages…

Example below works fine for me…

/***
   To retrieve your model and add new items from a GtkBuilder-loaded UI:

   GtkColumnView *view = GTK_COLUMN_VIEW(gtk_builder_get_object(builder, "column_view"));
   GtkSelectionModel *sel_model = gtk_column_view_get_model(view);
   GtkSingleSelection *single = GTK_SINGLE_SELECTION(sel_model);
   GListStore *store = G_LIST_STORE(gtk_single_selection_get_model(single));
   g_list_store_append(store, your_item);
 **/
static void run_additem_callback(GtkButton *button,
                                 GtkColumnView *column_view) {
  g_print("\nRun AddItem CallBack...!!!");

  g_print("\nValue of ColumnView: %p : ", column_view);

  GtkSelectionModel *selection_model = GTK_SELECTION_MODEL(gtk_column_view_get_model(column_view));
  //if (column_view != NULL) g_object_unref(column_view); // Can not unfef ColumnView, erros will happens

  /***
   * Get the Selected Item (for GtkSingleSelection): If using GtkSingleSelection,
   * retrieve the single selected item using gtk_single_selection_get_selected_item().
   **/
  if (GTK_IS_SINGLE_SELECTION(selection_model)) {

    GtkSingleSelection *single_selection = GTK_SINGLE_SELECTION(selection_model);
    //if (selection_model != NULL)  g_object_unref(selection_model); // Selection Model can not unref
    g_print("\nValue is Single Selectionw: %p : ", single_selection);

    // Get the underlying GListModel
    GListModel *list_model = gtk_single_selection_get_model(single_selection);
    //if (single_selection != NULL) g_object_unref(single_selection); // Single Selection can not unref
    g_print("\nValue of store: %p ", list_model);

    // Cast to GListStore
    GListStore *store = G_LIST_STORE(list_model);
    //if (list_model != NULL)g_object_unref(list_model); // List model can not unref, error will happens
    g_print("\nValue of store: %p ", store);

    // Create and append a new item
    BookData *new_item = book_data_new("Mrs",
                                       "Langston Vedenyakin",
                                       "Biography",
                                       "7/20/1913",
                                       "569314541-7",
                                       "Rupiah",
                                       "4.9",
                                       "Dabtype",
                                       "Tibetan",
                                      "894"); // Replace with your constructor
    g_list_store_append(store, new_item);
    //if (store != NULL) g_object_unref(store); // store can not unref, error will happens

    if (new_item != NULL) g_object_unref(new_item); // Store owns a reference now
  } else {
    g_print("\nIs not GtkSingleSelection");
  }

  g_print("\nFinish Run Add Item...!!!");
}
1 Like