GTKmm - 如何将 pixbuf 放入树视图中

发布于 2024-11-04 21:13:52 字数 1857 浏览 6 评论 0原文

我正在学习如何使用 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 技术交流群。

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

发布评论

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

评论(3

等数载,海棠开 2024-11-11 21:13:52

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

我有几个问题:您使用的是什么版本的 gtkmm?您是否在 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);

这有什么帮助吗?

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

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

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

Is that any help?

梦冥 2024-11-11 21:13:52

如果您想要的只是每行都有一个可能不同的库存图标,您可以通过设置 pixbuf 渲染器属性来实现。这个想法是该列包含一个字符串 stock-id,渲染器将其显示为图标。

// in your class declaration

struct columnsRecord : public Gtk::TreeModel::ColumnRecord {
    ...
    Gtk::TreeModelColumn<std::string>    stockID;   // stock id name
    ...
} treeColumns;

Gtk::CellRendererPixBuf     pixBufRenderer;
... 
// setting up columns
int numcols = treeView.append_column("Icon Column", pixBufRenderer);  // returns # cols after append
treeView.get_column(numcols-1)->add_attribute(pixBufRenderer,"stock-id",treeColumns.stockID)
...
// setting a row
std::string id = good_val ? Gtk::Stock::YES.id : Gtk::Stock::NO.id;
rowiter->set_value(columns.stockID,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.

// in your class declaration

struct columnsRecord : public Gtk::TreeModel::ColumnRecord {
    ...
    Gtk::TreeModelColumn<std::string>    stockID;   // stock id name
    ...
} treeColumns;

Gtk::CellRendererPixBuf     pixBufRenderer;
... 
// setting up columns
int numcols = treeView.append_column("Icon Column", pixBufRenderer);  // returns # cols after append
treeView.get_column(numcols-1)->add_attribute(pixBufRenderer,"stock-id",treeColumns.stockID)
...
// setting a row
std::string id = good_val ? Gtk::Stock::YES.id : Gtk::Stock::NO.id;
rowiter->set_value(columns.stockID,id);
Smile简单爱 2024-11-11 21:13:52

看起来有必要为 pixbuf 单元显式设置 CellRenderer。
此代码片段在每个数据行上显示“取消”图标。

Gtk::CellRendererPixbuf *cross = Gtk::manage(new Gtk::CellRendererPixbuf());
cross->property_stock_id() = Gtk::StockID(Gtk::Stock::CANCEL).get_string();
cross->property_stock_size() = Gtk::ICON_SIZE_BUTTON;
[...]
int cols_count = m_TreeView.append_column("icons", *cross);

要显示自定义图像,您需要删除两行设置 stock_id 添加如下内容:

Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count - 1);
if(pColumn) {
  pColumn->add_attribute(cell->property_value(), columns.store_pict);
}

Looks like it's necessary to explicitly set CellRenderer for pixbuf cells.
This code snippet displays CANCEL icon on each data row.

Gtk::CellRendererPixbuf *cross = Gtk::manage(new Gtk::CellRendererPixbuf());
cross->property_stock_id() = Gtk::StockID(Gtk::Stock::CANCEL).get_string();
cross->property_stock_size() = Gtk::ICON_SIZE_BUTTON;
[...]
int cols_count = m_TreeView.append_column("icons", *cross);

For displaying custom images you need to remove two lines setting stock_id add something like this below:

Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count - 1);
if(pColumn) {
  pColumn->add_attribute(cell->property_value(), columns.store_pict);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文