在构造函数中访问容器子窗口小部件的尺寸 [英] Accessing a container child widget dimensions in a constructor

查看:143
本文介绍了在构造函数中访问容器子窗口小部件的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含两个 Gtk :: Containers 的类,如下所示:

  class示例:public Gtk :: VBox 
{
public:
示例();
virtual〜Example();

私人:
Gtk :: HBox m_c1; //第一个容器
Gtk :: Grid m_c2; //第二个容器
};

构造一个示例以这种方式实现:

 示例:: Example()
{
const int nbRows {6};
const int nbColumns {7}; (int col {0}; col< nbColumns; ++ col)




















$ )};

m_c1.pack_start(* btn); (int col {0}; col< b
{
}

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

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

//布局设置:
pack_start(m_c1);
pack_start(m_c2);

show_all();
}

我想要做的是确保<$ c中的子窗口小部件为了确保两个容器之间的视觉一致性,$ c> m_c2 与 m_c1 中的子构件大小相同。否则,它看起来像这样:





只是为了让当然,我不希望让 m_c1 成为 Gtk :: Grid



下面是我到目前为止所尝试的:我使用 Gtk :: HBox 中的 get_child_at() code> m_c1 来获得对其第一个孩子的引用。然后,我在该子节点上调用 get_width() get_height()。这个想法是为这些维度提供 m_c2 的子窗口小部件。问题是返回的引用是 nullptr



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

解决方案

您需要将网格中的每个按钮设置为水平扩展。然后所有的尺码都会自行处理。

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?

解决方案

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

这篇关于在构造函数中访问容器子窗口小部件的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆