在 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;
“名称”列显示得很好,但没有组合框。我几乎肯定,仅仅将一个指向组合框的指针作为列类型是错误的,但我不知道它应该如何进行。我确实收到 GTK 警告:
GLib-GObject-WARNING **: unable to set property
text' of type
gchararray' from value of type `GtkComboBoxText'
这似乎表明(从一点点谷歌搜索),对于非基本类型没有默认的渲染器。但如果这是问题的话,我还没有找到任何如何设置的示例。所有教程仅显示具有原始数据类型的 TreeView。
有人知道如何将 ComboBox 放入 TreeView 中吗?
最佳答案
好吧,我还没有让它 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 )
关于c++ - 如何将 ComboBox 添加到 TreeView 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676942/