1

After spending a long time searching for an answer, I hope someone can help me with this problem. I'm trying to use gtkmm (version 3.14.0) and glade (version 3.18.3) on a Fedora 21 system to create a Gtk::TreeView/Gtk::ListStore with many small images. I can easily place stock icons in the list, but adding Gdk::Pixbuf objects seems to go wrong. No error or warning messages are shown, but the Gdk::Pixbuf image is not shown.

To show the problem, I've created a minimal working example (the code of the program and the glade file included at the end). Running this program should open a small window with the Gtk::TreeView with two "gtk-apply" icons. In the first column should be the icon added as Gdk::Pixbuf, in the second column should be the stock icon. However, when I run the program, the first column remains empty. There are no compile or run-time errors or warnings.

My final application will display a matrix of about 100 rows and about 35 columns of mostly tiny icons allowing a quick overview of activities done on the different days of a month. None of these icons will be stock icons.

Extra Information: Following the program execution using a debugger, I found that the Gtk::ListStore's first column wants data of type gtkmm__GdkPixbuf. The type of pb in the line row[cols.m_pb] = pb is GdkPixbuf. The type GdkPixbuf cannot be converted to gtkmm__GdkPixbuf automatically, causing the value to be set to 0 (NULL). Obviously this does not solve the problem, but might help to solve the problem.

Thanks for the help and best wishes for 2015, Wim

This is the file 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>

The file 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();
}
4

1 回答 1

0

在问题中的“额外信息 ”部分中显示的类型中发现问题后,我决定手动创建商店。的定义GtkListStore已从 glade 文件中删除。ws::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();
}

尽管这不像希望的那样灵活,但它确实解决了问题。

于 2015-01-03T18:51:39.523 回答