为什么在Gtk :: TreeView中看不到Gdk :: Pixbufs?

时间:2014-12-31 10:21:15

标签: treeview gtkmm glade pixbuf

花了很长时间寻找答案后,我希望有人可以帮我解决这个问题。我试图在Fedora 21系统上使用gtkmm(版本3.14.0)和glade(版本3.18.3)创建一个包含许多小图像的Gtk::TreeView / Gtk::ListStore。我可以轻松地将库存图标放在列表中,但添加Gdk::Pixbuf对象似乎出错了。没有显示错误或警告消息,但未显示Gdk::Pixbuf图像。

为了显示问题,我创建了一个最小的工作示例(程序代码和最后包含的glade文件)。运行此程序应打开一个小窗口,其中Gtk::TreeView有两个" gtk-apply"图标。在第一列中应添加为Gdk::Pixbuf的图标,在第二列中应为库存图标。但是,当我运行程序时,第一列仍为空。没有编译或运行时错误或警告。

我的最终应用程序将显示一个大约100行的矩阵和大约35列大多数小图标,可以快速概览一个月中不同日期的活动。这些图标都不是股票图标。

额外信息:在使用调试器执行程序后,我发现Gtk::ListStore的第一列需要gtkmm__GdkPixbuf类型的数据。行pbrow[cols.m_pb] = pb的类型为GdkPixbuf。类型GdkPixbuf无法自动转换为gtkmm__GdkPixbuf,导致该值设置为0(NULL)。显然这不能解决问题,但可能有助于解决问题。

感谢2015年的帮助和祝福, 维姆

这是文件mwe.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkAccelGroup" id="accelgroup1"/>
  <object class="GtkApplicationWindow" id="mainwindow">
    <property name="can_focus">False</property>
    <property name="show_menubar">False</property>
    <child>
      <object class="GtkGrid" id="mainbox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkTreeView" id="treattree">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="vexpand">True</property>
            <property name="hscroll_policy">natural</property>
            <property name="model">treatstore</property>
            <property name="rules_hint">True</property>
            <property name="search_column">0</property>
            <property name="fixed_height_mode">True</property>
            <child internal-child="selection">
              <object class="GtkTreeSelection" id="sel1"/>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="col1">
                <property name="sizing">fixed</property>
                <property name="fixed_width">32</property>
                <property name="title">1</property>
                <child>
                  <object class="GtkCellRendererPixbuf" id="cell1">
                    <property name="width">16</property>
                    <property name="height">16</property>
                  </object>
                  <attributes>
                    <attribute name="pixbuf">0</attribute>
                  </attributes>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkTreeViewColumn" id="col2">
                <property name="sizing">fixed</property>
                <property name="fixed_width">32</property>
                <property name="title">2</property>
                <child>
                  <object class="GtkCellRendererPixbuf" id="cell2"/>
                  <attributes>
                    <attribute name="stock-id">1</attribute>
                  </attributes>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkListStore" id="treatstore">
    <columns>
      <!-- column-name col1 -->
      <column type="GdkPixbuf"/>
      <!-- column-name col2 -->
      <column type="gchararray"/>
    </columns>
  </object>
</interface>

文件mwe.cpp:

#include <gtkmm.h>

namespace ws
{
class App : public Gtk::Application
{
protected:
    App() : Gtk::Application("nl.mwe.mwe"), m_mainwindow(0)
    {
    Glib::set_application_name("MWE");
    }

public:
    static Glib::RefPtr<App> create(int &argc, char **&argv)
    {
    return Glib::RefPtr<App>(new App());
    }
    void init(Glib::RefPtr<Gtk::Builder> builder);
    int run()
    {
    return Gtk::Application::run(*m_mainwindow);
    }
private:
    Gtk::ApplicationWindow *m_mainwindow;
};

// Definition of the column references
class ModelColumns : public Gtk::TreeModelColumnRecord
{
public:
    ModelColumns()
    {
    add(m_pb);
    add(m_stock);
    }
    Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > m_pb;
    Gtk::TreeModelColumn<Glib::ustring> m_stock;
};
static ModelColumns col;

} // End namespace ws

/**
 * \brief   Initialize the app
 * \param[in]       builder The builder object
 *
 * Here is where the list store is populated with the Gdk::Pixbuf
 */
void ws::App::init(Glib::RefPtr<Gtk::Builder> builder)
{
    builder->get_widget("mainwindow", m_mainwindow);
    m_mainwindow->show();

    Glib::RefPtr<Gtk::ListStore> store =
    Glib::RefPtr<Gtk::ListStore>::cast_static(
        builder->get_object("treatstore"));

    Gtk::TreeModel::Row row = *store->append();

    // The line below loads the stock icon as a pixbuf.
    Glib::RefPtr<Gdk::Pixbuf> pb =
    Gtk::IconTheme::get_default()->load_icon("gtk-apply", 16);
    row[col.m_pb] = pb;

    row[col.m_stock] = "gtk-apply";
}

int main (int argc, char *argv[])
{
    Glib::RefPtr<ws::App> myapp = ws::App::create(argc, argv);
    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create();
    builder->add_from_file("mwe.glade");
    myapp->init(builder);
    return myapp->run();
}

1 个答案:

答案 0 :(得分:0)

额外信息中找到类型中的问题后 在问题部分,我决定手工创建商店。定义 已从林间空地文件中删除了GtkListStorews::App::init() 方法改为:

void ws::App::init(Glib::RefPtr<Gtk::Builder> builder)
{
    builder->get_widget("mainwindow", m_mainwindow);

    Gtk::TreeView *treeview = 0;
    builder->get_widget("treattree", treeview);

    Glib::RefPtr<Gtk::ListStore> store =
    Gtk::ListStore::create(col);
    treeview->set_model(store);

    Gtk::TreeModel::Row row = *store->append();

    // The line below loads the stock icon as a pixbuf.
    Glib::RefPtr<Gdk::Pixbuf> pb =
        Gtk::IconTheme::get_default()->load_icon("gtk-apply", 16);
    row[col.m_pb] = pb;

    row[col.m_stock] = "gtk-apply";
    m_mainwindow->show();
}

虽然这并不像希望的那样灵活,但确实解决了这个问题。

相关问题