Chapter 10. ListView, GridView, ColumnView

Table of Contents

Lists are intended to be used whenever developers want to display many objects in roughly the same way. They are perfectly fine to be used for very short lists of only 2 or 3 items, but generally scale fine to thousands of items.

Lists are meant to be used with changing data, both with the items themselves changing as well as the list adding and removing items. Of course, they work just as well with static data.

The List Widget Overview chapter in the GTK documentation contains more information about list widgets.

Some examples are shown in this chapter. There are more examples in the listmodelviews directory in gtkmm-documentation's examples.

The Data Model

The data model is a class that implements the Gio::ListModel interface. Examples of such classes are Gio::ListStore (not to be confused with the deprecated Gtk::ListStore), Gtk:StringList, Gtk:DirectoryList and Pango::FontMap.

The elements in a model are called items. All items are instances of a subclass of Glib::Object. For instance, you might have a ColumnView with one integer and one text column, like so:

class ModelColumns : public Glib::Object
{
public:
  int m_col_id;
  Glib::ustring m_col_name;

  static Glib::RefPtr<ModelColumns> create(
    int col_id, const Glib::ustring& col_name)
  {
    return Glib::make_refptr_for_instance<ModelColumns>(
      new ModelColumns(col_id, col_name));
  }

protected:
  ModelColumns(int col_id, const Glib::ustring& col_name)
  : m_col_id(col_id), m_col_name(col_name)
  {}
};

Glib::RefPtr<Gio::ListStore<ModelColumns>> m_ListStore;

Every item in a model has a position which is the unsigned integer that describes where in the model the item is located. The first item in a model is at position 0. The position of an item can of course change as other items are added to or removed from the model.

Gio::ListStore Reference

StringList Reference

DirectoryList Reference