c++ - GTKmm - 如何将 pixbuf 放入 TreeView 中

标签 c++ glade gtkmm gtktreeview gdkpixbuf

我正在学习如何使用 GTKmm,但我真的很难弄清楚如何将图像放入 TreeView 中。我使用 Glade 创建了一个包含 3 列的树存储,其中一列是名为 store_pixbufGdkPixbuf。我还在 glade 中创建了一个 treeview,其中有一列包含一个名为 int_col_pict 的 pixbuf 单元格渲染器和一个 char 数组单元格渲染器。在我的代码中,我对 treestore 有通常的 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

Click here看看它运行的样子。该图像应该与“FastEthernet...”行位于同一行

有谁知道我该如何解决这个问题?我是不是完全错了?感谢您的浏览,感谢您提供的每一点帮助!

最佳答案

您的代码看起来是正确的。我只是编写了一个简单的示例,使用 gtkmm-2.4,我可以毫无问题地为 Glib::RefPtr 创建一个列

我有几个问题:您使用的是什么版本的 gtkmm?您是否在 TreeView 中为 Pixbuf 添加了一个列?

我不会发布我的完整示例,但相关的部分是:

在example.h中

  //Tree model columns:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_col_store_pict);}
    Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > m_col_store_pict;

  };

  ModelColumns m_Columns;

在 example.cpp 中

  //Create the Tree model:
  m_refTreeModel = Gtk::ListStore::create(m_Columns);
  m_TreeView.set_model(m_refTreeModel);

  //Fill the TreeView's model
  Gtk::TreeModel::Row row = *(m_refTreeModel->append());
  row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/arts.png");

  row = *(m_refTreeModel->append());
  row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/fonts.png");

  row = *(m_refTreeModel->append());
  row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/access.png");

  //Add the TreeView's view columns:
  m_TreeView.append_column("Some Picture", m_Columns.m_col_store_pict);

这有什么帮助吗?

关于c++ - GTKmm - 如何将 pixbuf 放入 TreeView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5894344/

相关文章:

C++ 异步线程

python - Glade/GTK+3/Python 中的 ComboBoxText - 第二次显示时消失

c - Glade UI 构建器不允许我将对象分配为信号参数

c++ - GTK+/Gtkmm,检查notebook标签页是否已经存在,不存在才创建

c++ - 如何激活 TreeView 中的单元格进行编辑

c++ - 如何为 gtkmm 应用程序创建 CMakeLists.txt?

c++ - 为什么 std::reference_wrapper 不是默认可构造的?

c++ - QFile::copy 是否保留复制文件中的源文件权限?

c++ - 使用鼠标选择轮廓?

c++ - GtkTreeView 交替行颜色