Zobrazení (View)
View je skutečný widget (Gtk::TreeView), který zobrazuje data modelu (Gtk::TreeModel) a umožňuje uživateli s nimi komunikovat. Může zobrazovat všechny sloupce modelu, nebo jen vybrané, a umí je zobrazovat různými způsoby.
- 10.2.1. Použití modelu
- 10.2.2. Přidání sloupců do zobrazení
- 10.2.3. Více než jeden sloupec modelu ve sloupci zobrazení
- 10.2.4. Určení údajů pro CellRenderer
- 10.2.5. Editovatelné buňky
10.2.1. Použití modelu
Gtk::TreeModel můžete určit, když konstruhujete Gtk::TreeView, nebo můžete použít metodu set_model(), nějak takto:
m_TreeView.set_model(m_refListStore);
10.2.2. Přidání sloupců do zobrazení
Můžete použít metodu append_column(), abyste řekli widgetu View, že má použít konkrétní sloupce objektu Modelu, v konkrétním pořadí, s konkrétními záhlavími sloupců.
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.
10.2.3. Více než jeden sloupec modelu ve sloupci zobrazení
Když chcete v jednom sloupci zobrazení vykreslit více než jeden sloupec z modelu, musíte vytvořit widget TreeView::Column ručně a k přidání sloupce modelu do něj použít pack_start().
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.
Here is some example code, which has a pixbuf icon and a text name in the same column:
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);
10.2.4. Určení údajů pro CellRenderer
Výchozí CellRenderers a jeho výchozí chování běžně postačují, ale občas může být potřeba preciznější ovládání. Například tento ukázkový kód z gtkmm/demos/gtk-demo/example_treeview_treestore.cc připojuje widget Gtk::CellRenderer a říká mu, aby vykresloval data z různých sloupců modelu podle různých hledisek jejich podoby.
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);
Můžete také napojit signál CellRenderer, abyste odhalili uživatelské činnosti. Například:
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) );
10.2.5. Editovatelné buňky
10.2.5.1. Editovatelné buňky s automatickým ukládáním.
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.
10.2.5.2. Implementace vlastní logiky pro editovatelné buňky.
Může se stát, že budete chtít, aby se nové hodnoty neukládaly okamžitě. Například můžete chtít omezit vstup na konkrétní znaky nebo rozsah hodnot.
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.
Měli byste pak Gtk::CellRenderer* přetypovat na konkrétní CellRenderer, který očekáváte, abyste mohli použít jeho API.
Například, pro CellRendererText byste mohli nastavit vlastnost buňky editable na true takto:
cell->property_editable() = true;
U CellRendererToggle byste místo toho mohli nastavit vlastnost 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.
Ve své obsluze signálu byste měli zkontrolovat novou hodnotu a uložit ji do objektu Model, pokud se to vaší aplikaci může hodit.