我正在學習如何使用GTKmm,並且我很難搞清楚如何將圖像放入樹視圖。我用Glade創建了3列的樹庫,其中一個是GdkPixbuf
,名爲store_pixbuf
。我還在林間空地創建了一個樹狀視圖,列中既有一個名爲int_col_pict
的pixbuf單元格渲染器,也有一個char數組單元格渲染器。在我的代碼,我有喜歡的treestore通常MyColumns
定義:GTKmm - 如何在樹視圖中放置一個pixbuf
class MyModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
Gtk::TreeModelColumn<Glib::ustring> store_hostname;
Gtk::TreeModelColumn<Glib::ustring> store_intname;
Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > store_pict;
MyModelColumns () { add(store_hostname); add(store_intname); add(store_pict);}
};
,並使用下面的代碼位來填充它。
//Get a pointer to the treestore
Glib::RefPtr<Gtk::TreeStore> treestore = Glib::RefPtr<Gtk::TreeStore>::cast_static(builder->get_object("routerTreeStore"));
//make sure the pointer isn't bad
if(treestore){
MyModelColumns columns;
//populate the first column
Gtk::TreeRow row= *(treestore->append());
row[columns.store_hostname] = router->hostname;
//populate all children
for(int i=0; i<router->interfaces.size(); i++)
{
//append child row
Gtk::TreeRow child = *(treestore->append(row.children()));
//insert data into the row
child[columns.store_pict] = Gdk::Pixbuf::create_from_file("red_dot.png");
child[columns.store_intname] = router->interfaces[i].interfaceName;
}
}//if
我最初試圖用股票的形象,但我想不出我應該使用什麼功能,所以後來我試圖用Gdk::Pixbuf::create_from_file()
(你可以在上面看到),但在運行時我得到以下錯誤:
Gtk-WARNING **: gtktreestore.c:765: Unable to convert from GdkPixbuf to gtkmm__GdkPixbuf
Click here看看它看起來像什麼樣的運行。該圖像應該與「FastEthernet ...」行在同一行上
有誰知道我該如何解決這個問題?我是否完全錯誤?感謝您的期待,每一點幫助表示感謝!
感謝您的迴應! 我正在使用GTKmm 2.4。我最初並沒有爲pixbuf使用單獨的列(我看到一個GTK教程就是這樣做的,我只是假設它會起作用),但是由於您做了不同的嘗試,我試圖將其更改爲自己的列。它仍然不起作用,並且我得到相同的錯誤信息。 – bmw357 2011-05-06 00:38:35