花了很长时间寻找答案后,我希望有人能帮助我解决这个问题。我试图在Fedora 21系统上使用gtkmm(版本3.14.0)和glade(版本3.18.3)来创建带有许多小图片的Gtk::TreeView
/Gtk::ListStore
。我可以很容易地将股票图标放入列表中,但添加Gdk::Pixbuf
对象似乎出错了。没有显示错误或警告消息,但未显示Gdk::Pixbuf
图像。为什么Gdk :: Pixbufs在Gtk :: TreeView中不可见?
为了显示这个问题,我创建了一个最小的工作示例(程序代码和最后包含的glade文件)。运行这个程序应该打开一个带有两个“gtk-apply”图标的小窗口Gtk::TreeView
。在第一列中应该将图标添加为Gdk::Pixbuf
,在第二列中应该是股票图标。但是,当我运行该程序时,第一列保持空白。没有编译或运行时错误或警告。
我的最终应用程序将显示一个大约100行和大约35列大多数小图标的矩阵,从而可以快速了解在一个月中不同日期所完成的活动。这些图标都不是股票图标。
额外信息:按照调试程序的执行,我发现Gtk::ListStore
的第一列要gtkmm__GdkPixbuf
类型的数据。行row[cols.m_pb] = 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();
}
我为这个问题提交了一个错误报告[https://bugzilla.gnome.org/show_bug.cgi?id=742637]。 – sawims