在构造函数中访问容器 child 小部件维度

Accessing a container child widget dimensions in a constructor

假设我有一个 class 和两个 Gtk::Containers 像这样:

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

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

构造一个Example是这样实现的:

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

我想做的是确保m_c2中的child小部件与m_c1中的child小部件大小相同,以确保视觉一致性在两个容器之间。否则,它看起来像这样:

只是为了确定,我不希望 m_c1 成为 Gtk::Grid

这是我目前尝试过的方法:我使用 Gtk::HBox m_c1 中可用的 get_child_at() 方法来获取对其第一个 child 的引用。然后我在 child 上调用了 get_width()get_height()。这个想法是为 m_c2 的 child 小部件提供这些维度。问题是返回的引用是 nullptr.

从我读过的一些帖子来看,我的小部件似乎还没有实现,这可以解释我的困难。我怎样才能做到这一点?

您需要将网格中的每个按钮设置为expand horizontally。然后所有的大小都会自行处理。