如何将ComboBox添加到TreeView列?

时间:2011-12-30 06:49:00

标签: c++ gtkmm

在Gtkmm中,我希望有一个带有ListStore的Gtk TreeView,并且列表中的一列是ComboBoxText。但我似乎无法弄明白该怎么做。

我目前看起来像:

class PlayerListColumns : public Gtk::TreeModelColumnRecord
{
public:

    PlayerListColumns()
    { add(name); add(team);}

    TreeModelColumn<string> name;
    TreeModelColumn<ComboBoxText*> team;
}

然后设置TreeView(player_list_view对象)

PlayerListColumns *columns = new PlayerListColumns();
Glib::RefPtr<ListStore> refListStore = ListStore::create(*columns);
player_list_view->set_model(refListStore);

ComboBoxText *box = manage(new ComboBoxText());
box->append("Blah");
box->append("Blah");
box->append("Blah");

TreeModel::Row row = *(refListStore->append());
row[columns->name] = "My Name";
row[columns->team] = box;

列“名称”显示正常,但没有ComboBox。我几乎肯定只是有一个指向组合框的指针,因为列类型是错误的,但我不知道它应该如何去。我确实得到了GTK警告:

  

GLib-GObject-WARNING **:无法从类型为“GtkComboBoxText”的值设置属性text' of type gchararray'

这似乎表明(从一小部分谷歌搜索)没有非基本类型的默认渲染器。但是,如果那是问题,我无法找到如何设置一个的例子。所有教程仅显示具有原始数据类型的TreeView。

任何人都知道如何将ComboBox放入TreeView?

1 个答案:

答案 0 :(得分:3)

好的,我没有让它100%工作,但这个示例类应该让你走在正确的轨道上: http://svn.gnome.org/svn/gtkmm-documentation/trunk/examples/book/treeview/combo_renderer/

基本上,您需要在列类中添加Gtk::TreeModelColumn<Glib::RefPtr<Gtk::ListStore> >,并在Gtk::TreeModelColumn<string>中保存所选数据。

然后,要使列成为组合框,您必须添加:

//manually created column for the tree view
Gtk::TreeViewColumn* pCol = Gtk::manage(new Gtk::TreeViewColumn("Choose"));

//the combobox cell renderer
Gtk::CellRendererCombo* comboCell = Gtk::manage(new Gtk::CellRendererCombo);

//pack the cell renderer into the column
pCol->pack_start(*comboCell);

//append the column to the tree view
treeView->append_column(*pCol);

//this sets the properties of the combobox and cell
//my gtkmm seems to be set for Glibmm properties
#ifdef GLIBMM_PROPERTIES_ENABLED
   pCol->add_attribute(comboCell->property_text(), columns->team);

   //this is needed because you can't use the ComboBoxText shortcut
   // you have to create a liststore and fill it with your strings separately
   // from your main model
   pCol->add_attribute(comboCell->property_model(), columns->teams);

   comboCell->property_text_column() = 0;
   comboCell->property_editable() = true;
#else
   pCol->add_attribute(*comboCell, "text", columns->team);
   pCol->add_attribute(*comboCell, "model", columns->teams);
   comboCell->set_property(text_column:, 0);
   comboCell->set_property("editable", true);
#endif

//connect a signal so you can set the selected option back into the model
//you can just have a column that is not added to the view if you want
comboCell->signal_edited()
    .connect(sigc::mem_fun(*this,&ComboWindow::on_combo_choice_changed));

以上编辑

我认为使用Gtk::CellRendererCombo*的方法是PlayerListColumns

的方法

http://developer.gnome.org/gtkmm/stable/classGtk_1_1CellRendererCombo.html

(我还没有进行过工作测试,但我从以下方面得到了这个想法: http://developer.gnome.org/gtkmm-tutorial/unstable/sec-treeview.html.en#treeview-cellrenderer-details