Accessing a container child widget dimensions in a

2019-07-08 09:54发布

问题:

Let's say I have a class with two Gtk::Containers like so:

class Example : public Gtk::VBox
{
public:
    Example();
    virtual ~Example();

private:
    Gtk::HBox m_c1; // First container
    Gtk::Grid m_c2; // Second container
};

Constructing an Example is implemented this way:

Example::Example()
{
    const int nbRows{6};
    const int nbColumns{7};

    for(int col{0}; col < nbColumns; ++col)
    {
        Gtk::Button* btn {new Gtk::Button("A")};

        m_c1.pack_start(*btn);
    }

    for(int row{0}; row < nbRows; ++row)
    {
        for(int col{0}; col < nbColumns; ++col)
        {
            Gtk::Button* btn {new Gtk::Button("A")};

            m_c2.attach(*btn, col, row, 1, 1);
        }
    }

    // Layout setup:
    pack_start(m_c1);
    pack_start(m_c2);

    show_all();
}

What I want to do is to make sure the child widgets in m_c2 are the same size as the child widget in m_c1, to ensure visual consistency between the two containers. Otherwise, it looks like this:

Just to make sure, I do not want m_c1 to be made a Gtk::Grid.

Here's what I have tried so far: I used the get_child_at() method available from the Gtk::HBox m_c1 to get a reference to its first child. I then called get_width() and get_height() on that child. The idea was to feed m_c2's child widgets these dimensions. The problem is that the returned reference is a nullptr.

From some posts I have read, it seems my widgets may no yet be realized, and that would explain my difficulties. How could I achieve this?

回答1:

You need to set each button in the grid to expand horizontally. Then all the sizing will take care of itself.



标签: c++ gtk gtkmm