如何将 ComboBox 添加到 TreeView 列?

发布于 2024-12-23 21:54:22 字数 1166 浏览 2 评论 0 原文

在 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;

“name”列显示得很好,但没有 ComboBox。我几乎肯定,仅仅将一个指向组合框的指针作为列类型是错误的,但我不知道它应该如何进行。我确实收到 GTK 警告:

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

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

有人知道如何将 ComboBox 放入 TreeView 中吗?

In Gtkmm, I want to have a Gtk TreeView with a ListStore, and have one of the columns in the list be a ComboBoxText. But I can't seem to figure out how to do it.

What I currently have looks like:

class PlayerListColumns : public Gtk::TreeModelColumnRecord
{
public:

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

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

Then when setting the TreeView (the player_list_view object)

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;

The column "name" shows up just fine, but no ComboBox. I'm almost positive that simply having a pointer to a combo box as the column type is wrong, but i don't know how it's supposed to go. I do get GTK warning:

GLib-GObject-WARNING **: unable to set property text' of typegchararray' from value of type `GtkComboBoxText'

Which seems to indicate (from a small bit of Googling) that there isn't a default renderer for non-basic types. But I haven't been able to find any examples of how to set one up, if that were the problem. All the tutorials only show TreeViews with primitive data types.

Anyone know how to put a ComboBox into a TreeView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-12-30 21:54:22

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

基本上你需要添加一个Gtk::TreeModelColumn > 到您的列类,并使用 Gtk::TreeModelColumn 来保存所选数据。

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

//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)

Okay, I haven't gotten it working 100%, but this example class should get you on the right track:
http://svn.gnome.org/svn/gtkmm-documentation/trunk/examples/book/treeview/combo_renderer/

Basically you need to add a Gtk::TreeModelColumn<Glib::RefPtr<Gtk::ListStore> > to your columns class and a Gtk::TreeModelColumn<string> to hold the selected data.

Then, to make a column a combobox, you have to add:

//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));

EDIT ABOVE

I think something along the lines of using a Gtk::CellRendererCombo* is the way to go in your PlayerListColumns

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

(I haven't made a working test yet, but I got the idea from:
http://developer.gnome.org/gtkmm-tutorial/unstable/sec-treeview.html.en#treeview-cellrenderer-details)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文