cgtkgtk4

How can I make g_list_store_append know in which GtkColumnView append the string in gtk4


This code shows how I created the column view:

GListStore *store = g_list_store_new (GTK_TYPE_STRING_OBJECT);
GtkSelectionModel *selection = GTK_SELECTION_MODEL(gtk_single_selection_new (G_LIST_MODEL(store)));
GtkWidget *view = gtk_column_view_new (selection);
// begin col1:
GtkListItemFactory *factory = gtk_signal_list_item_factory_new ();
g_signal_connect (factory, "setup", G_CALLBACK (cb_setup_col1), NULL);
g_signal_connect (factory, "bind", G_CALLBACK (cb_bind_col1), NULL);
GtkColumnViewColumn *column1 = gtk_column_view_column_new ("Archivo", factory);
gtk_column_view_append_column (GTK_COLUMN_VIEW (view), column1);
// end col1
// begin col2:
GtkColumnViewColumn *column2 = gtk_column_view_column_new ("Formato", factory);
gtk_column_view_append_column (GTK_COLUMN_VIEW (view), column2);
// end col2

g_autoptr(GtkStringObject) strobj1 = gtk_string_object_new ("str_for_col1");
g_autoptr(GtkStringObject) strobj2 = gtk_string_object_new ("str_for_col2");
g_list_store_append (store, strobj1);
g_list_store_append (store, strobj2);

//...
void cb_setup_col1 (GtkSignalListItemFactory *self, GtkListItem *list_item, gpointer user_data)
{
    GtkWidget *label = gtk_label_new ("");
    gtk_list_item_set_child (list_item, label);
}

void cb_bind_col1 (GtkSignalListItemFactory *self, GtkListItem *list_item, gpointer user_data)
{
    GtkStringObject *strobj = gtk_list_item_get_item (list_item);
    GtkWidget *label = gtk_list_item_get_child (list_item);
    gtk_label_set_text (GTK_LABEL(label), gtk_string_object_get_string (strobj));
}

The thing is that I realize that I have one model with the type string for both columns, not like gtk_list_store_set which I can tell the position to insert the string. Any ideas about how to insert strobj2 as item for the second column?


Solution

  • In order to create a column view with several columns, this should already be taken into account in the ListStore.

    For example like this:

    #define CAPITAL_TYPE_ITEM (capital_item_get_type())
    G_DECLARE_FINAL_TYPE (CapitalItem, capital_item, CAPITAL, ITEM, GObject)
    
    struct _CapitalItem
    {
        GObject parent_instance;
        const char *name;
        const char *country;
        const char *founded;
        const char *population;
        const char *area;
    };
    
    struct _CapitalItemClass
    {
        GObjectClass parent_class;
    };
    
    G_DEFINE_TYPE (CapitalItem, capital_item, G_TYPE_OBJECT)
    
    static void capital_item_init(CapitalItem *item)
    {
    }
    
    static void capital_item_class_init(CapitalItemClass *class)
    {
    }
    
    static CapitalItem * capital_item_new(const char *name,
                    const char *country,
                    const char *founded,
                    const char *population,
                    const char *area )
    {
        CapitalItem  *item = g_object_new(CAPITAL_TYPE_ITEM, NULL);
        item->name = g_strdup(name);
        item->country = g_strdup(country);
        item->founded = g_strdup(founded);
        item->population = g_strdup(population);
        item->area = g_strdup(area);
        return item;
    }
    
    // a funktion that creates a GListModel with capital_item  objects
    GListModel * create_capital_model(void)
    {
        GListStore *store = g_list_store_new(G_TYPE_OBJECT);
        g_list_store_append(store, capital_item_new("Washington,D.C.","USA","1790","712,816","177"));
        g_list_store_append(store, capital_item_new("London","Britain","43","8,799,800","1,572.03"));
        g_list_store_append(store, capital_item_new("Paris","France","3rd c. BC","2,161,000","105"));
        g_list_store_append(store, capital_item_new("Berlin","Germany","13th century","3,850,809","891.3"));
        g_list_store_append(store, capital_item_new("Rome","Italy","753 BC","2,860,009","1,285"));
        return G_LIST_MODEL(store);
    }
    

    Furthermore, a factory is needed for each column.

    A complete example can be found here:

    https://stackoverflow.com/a/77759424/22768315

    How an entry selected with the mouse can be determined and changed is shown here.

    https://stackoverflow.com/a/77619798/22768315

    One option for sorting is shown here:

    https://stackoverflow.com/a/78185115/22768315

    Have fun continuing to code