gpt4 book ai didi

c++ - 如何将 ComboBox 添加到 TreeView 列?

转载 作者:搜寻专家 更新时间:2023-10-31 01:55:11 26 4
gpt4 key购买 nike

在 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 typegchararray' 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com