Gtkmm::View[2]

More than one Model Column per View Column

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::View::append_column() is overridden to accept either a prebuilt Gtk::View::Column widget, or just the TreeModelColumn from which it generates an appropriate Gtk::View::Column widget.
Here is some example code from demos/gtk-demo/example_stockbrowser.cc, which has a pixbuf icon and a text name in the same column:
Gtk::TreeView::Column* pColumn =
    Gtk::manage(new Gtk::TreeView::Column("Symbol") );
// m_columns.icon and m_columns.symbol are columns in the model.
// pColumn is the column in the TreeView:
pColumn->pack_start(m_columns.icon, false); //false = don't expand.
pColumn->pack_start(m_columns.symbol);
m_TreeView.append_column(*pColumn);
 
Remark: 每一列是一个Gtk::View::Column的控件,显然是View里面的与Model无关。再用append_column生成的时候可以用pre-built的Gtk::View::Column
 

Specifying CellRenderer details

The default CellRenderers and their default behaviour will normally suffice, but you might occasionally need finer control. For instance, this example code from demos/gtk-demo/example_treestore.cc, manually constructs a Gtk::CellRenderer widget and instructs it to render the data from various model columns through various aspects of its appearance.
Gtk::CellRendererToggle* pRenderer =  Gtk::manage( new Gtk::CellRendererToggle() );
int cols_count = m_TreeView.append_column("Alex", *pRenderer);
Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
if(pColumn)
{
  pColumn->add_attribute(pRenderer->property_active(),
      m_columns.alex);
  pColumn->add_attribute(pRenderer->property_visible(),
      m_columns.visible);
  pColumn->add_attribute(pRenderer->property_activatable(),
      m_columns.world);
You can also connect to CellRenderer signals to detect user actions. For instance:
Gtk::CellRendererToggle* pRenderer =
    Gtk::manage( new Gtk::CellRendererToggle() );
pRenderer->signal_toggled().connect(
    sigc::bind( sigc::mem_fun(*this,
        &Example_TreeView_TreeStore::on_cell_toggled), m_columns.dave)
);

自己新建的Cell Render显然也是从Model中读取数据,而Model的Column就是制定从哪个中读取数据,充当桥梁作用。依赖的所有数据都来源于Model。继而Model Column大约的作用仅仅是用自己的成员变量当做标志位。也就是可以在两者之间进行连接。

而自己做出的CellRenderer可以用add_attrbute来把本身的某个property同model中的某列对应。

实际上相当于model的对象做出一张数据表,不需要考虑行对应问题,只需要考虑列对应问题就好了,而列标识符就为一个Model Column的实例。而View Column则是一个Sensor,可以定制其Sensor,Model中的哪一个列。

估计还有的东西就是响应相应的CellRenderer的signal和定制行相关的东西,主要是selection和filter。

 

posted on 2008-12-21 01:47  壶中仙  阅读(228)  评论(0)    收藏  举报

导航