The View
The View is the actual widget (Gtk::TreeView) that displays the model (Gtk::TreeModel) data and allows the user to interact with it. The View can show all of the model's columns, or just some, and it can show them in various ways.
Using a Model
Du kan ange en Gtk::TreeModel när du skapar din Gtk::TreeView, eller så kan du använda metoden set_model(), så här:
m_TreeView.set_model(m_refListStore);
Adding View Columns
You can use the append_column() method to tell the View that it should display certain Model columns, in a certain order, with a certain column title.
m_TreeView.append_column("Messages", m_Columns.m_col_text);
When using this simple append_column() overload, the TreeView will display the model data with an appropriate CellRenderer. For instance, strings and numbers are shown in a simple Gtk::Entry widget, and booleans are shown in a Gtk::CheckButton. This is usually what you need. For other column types you must either connect a callback that converts your type into a string representation, with TreeViewColumn::set_cell_data_func(), or derive a custom CellRenderer. Note that (unsigned) short is not supported by default - You could use (unsigned) int or (unsigned) long as the column type instead.
Mer än en modellkolumn per vykolumn
To render more than one model column in a view column, you need to create the TreeView::Column widget manually, and use pack_start() to add the model columns to it.
Then use append_column() to add the view Column to the View. Notice that Gtk::TreeView::append_column() is overloaded to accept either a prebuilt Gtk::TreeView::Column widget, or just the TreeModelColumn from which it generates an appropriate Gtk::TreeView::Column widget.
Här är lite exempelkod som har en pixbuf-ikon och ett textnamn i samma kolumn:
auto pColumn = Gtk::make_managed<Gtk::TreeView::Column>("Icon Name");
// m_columns.icon and m_columns.iconname are columns in the model.
// pColumn is the column in the TreeView:
pColumn->pack_start(m_columns.icon, /* expand= */ false);
pColumn->pack_start(m_columns.iconname);
m_TreeView.append_column(*pColumn);
Ange detaljer för CellRenderer
The default CellRenderers and their default behavior will normally suffice, but you might occasionally need finer control. For instance, this example code from gtkmm/demos/gtk-demo/example_treeview_treestore.cc, appends a Gtk::CellRenderer widget and instructs it to render the data from various model columns through various aspects of its appearance.
auto cols_count = m_TreeView.append_column_editable("Alex", m_columns.alex);
auto pColumn = m_TreeView.get_column(cols_count-1);
if(pColumn)
{
auto pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);
pColumn->add_attribute(pRenderer->property_activatable(), m_columns.world);
Du kan också ansluta till CellRenderer-signaler för att upptäcka användaråtgärder. Till exempel:
auto pRenderer = Gtk::make_managed<Gtk::CellRendererToggle>();
pRenderer->signal_toggled().connect(
sigc::bind( sigc::mem_fun(*this,
&Example_TreeView_TreeStore::on_cell_toggled), m_columns.dave)
);
Redigerbara celler
Automatiskt lagrade redigerbara celler.
Cells in a TreeView can be edited in-place by the user. To allow this, use the Gtk::TreeView insert_column_editable() and append_column_editable() methods instead of insert_column() and append_column(). When these cells are edited the new values will be stored immediately in the Model. Note that these methods are templates which can only be instantiated for simple column types such as Glib::ustring, int, and long.
Implementera anpassad logik för redigerbara celler.
Du vill dock kanske inte att de nya värdena ska lagras omedelbart. Du vill exempelvis kanske begränsa inmatningen till vissa tecken eller teckenintervall.
To achieve this, you should use the normal Gtk::TreeView insert_column() and append_column() methods, then use get_column_cell_renderer() to get the Gtk::CellRenderer used by that column.
You should then cast that Gtk::CellRenderer* to the specific CellRenderer that you expect, so you can use specific API.
För en CellRendererText skulle du exempelvis ställa in cellens editable-egenskap till true, så här:
cell->property_editable() = true;
För en CellRendererToggle skulle du i stället ställa in egenskapen activatable.
You can then connect to the appropriate "edited" signal. For instance, connect to Gtk::CellRendererText::signal_edited(), or Gtk::CellRendererToggle::signal_toggled(). If the column contains more than one CellRenderer then you will need to use Gtk::TreeView::get_column() and then call get_cells() on that view Column.
In your signal handler, you should examine the new value and then store it in the Model if that is appropriate for your application.