2011-12-30 74 views
2

在Gtkmm中,我想要一個帶有ListStore的Gtk TreeView,並且列表中的一列是ComboBoxText。但我似乎無法弄清楚如何去做。如何將組合框添加到TreeView列?

我目前擁有的模樣:

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 **:無法設置屬性text' of type gchararray '從類型'值GtkComboBoxText'

這似乎表明(從一個小一點谷歌搜索),沒有非基本類型的默認渲染器。但是,如果這是問題,我一直無法找到如何設置一個例子。所有教程僅顯示具有原始數據類型的TreeView。

任何人都知道如何將一個ComboBox放到TreeView中?

回答

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