Search code examples
c++gtkmm

What is the correct way of adding Image to Grid in gtkmm?


Having a Gtk::Grid and attempting to do the following:

Gtk::Image *im;
for(int i=0; i<10; ++i)
{
    for(int j=0; j<10; ++j)
    {
        im = Gtk::manage(new Gtk::Image());
        im->set("test.jpeg");
        grid->attach(*im, i, j, 40, 40);
    }
}

The problem is that it generates something very strange:

Strange result

The goal is to have the same image added separately.


Solution

  • The default description of Gtk::Grid::attach() is confusing. The last two attributes are labeled as "width" and "height" and it can be expected to mean the pixel dimentions of an added Widget. However, this is not what these parameters mean at all.

    In reality they mean something closer to the "span". Substituting these values to 1 yields correct results.

    Gtk::Image *im;
    for(int i=0; i<10; ++i)
    {
        for(int j=0; j<10; ++j)
        {
            im = Gtk::manage(new Gtk::Image());
            im->set("test.jpeg");
            grid->attach(*im, i, j, 1, 1);
        }
    }