GTKmm - 如何将 pixbuf 放入树视图中
我正在学习如何使用 GTKmm,但我很难弄清楚如何将图像放入树视图中。我使用 Glade 创建了一个包含 3 列的树存储,其中一列是一个名为 store_pixbuf
的 GdkPixbuf。我还在 Glade 中创建了一个树视图,其中有一列包含一个名为 int_col_pict
的 pixbuf 单元格渲染器和一个 char 数组单元格渲染器。在我的代码中,我对树存储有通常的 MyColumns
定义,例如:
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
单击此处查看它的样子跑步。该图像应该与“FastEthernet...”行位于同一行
有谁知道我如何解决这个问题?我的做法完全错误吗?感谢您的浏览,感谢您的每一点帮助!
I'm learning how to use GTKmm and I'm having a really hard time figuring out how to put an image into a treeview. I used Glade to create a treestore with 3 columns, one of which is a GdkPixbuf
called store_pixbuf
. I also created a treeview in glade, with a column that has both a pixbuf cell renderer called int_col_pict
and a char array cell renderer. In my code, I have the usual MyColumns
definition for the treestore like:
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);}
};
and use the following bit of code to populate it.
//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
I initially tried to use a stock image, but I could not figure out what function I was supposed to use, so then I tried to use Gdk::Pixbuf::create_from_file()
(as you can see above), but at run time I get the following error:
Gtk-WARNING **: gtktreestore.c:765: Unable to convert from GdkPixbuf to gtkmm__GdkPixbuf
Click here to see what it looks like running. The image is supposed to go on the same line as the "FastEthernet..." lines
Does anyone know how I can solve this? Am I going about it completely wrong? Thanks for looking, every little bit of help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码看起来是正确的。我刚刚编写了一个快速示例,使用 gtkmm-2.4,我可以毫无问题地为 Glib::RefPtr 创建列。
我有几个问题:您使用的是什么版本的 gtkmm?您是否在 Pixbuf 的树形视图中添加一列?
我不会发布完整的示例,但相关位是:
在 example.h
在 example.cpp
这有什么帮助吗?
Your code looks correct. I just coded up a quick example and with gtkmm-2.4, I have no problems creating a column for a Glib::RefPtr
I couple of questions: what version of gtkmm are you using? Are you adding a column in the treeview for the Pixbuf?
I won't post my complete example but the relevant bits are:
in example.h
in example.cpp
Is that any help?
如果您想要的只是每行都有一个可能不同的库存图标,您可以通过设置 pixbuf 渲染器属性来实现。这个想法是该列包含一个字符串 stock-id,渲染器将其显示为图标。
If all you want is a potentially different stock icon for each row, you can do it by setting an pixbuf renderer attribute. The idea is that the column contains a string stock-id, which the renderer displays as an icon.
看起来有必要为 pixbuf 单元显式设置 CellRenderer。
此代码片段在每个数据行上显示“取消”图标。
要显示自定义图像,您需要删除两行设置 stock_id 添加如下内容:
Looks like it's necessary to explicitly set CellRenderer for pixbuf cells.
This code snippet displays CANCEL icon on each data row.
For displaying custom images you need to remove two lines setting stock_id add something like this below: