Accessing a container child widget dimensions in a constructor

左心房为你撑大大i 提交于 2019-12-13 16:49:58

问题


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.



来源:https://stackoverflow.com/questions/48477670/accessing-a-container-child-widget-dimensions-in-a-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!