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.
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…
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.
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.
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...!!!");
}