简体   繁体   English

gtkmm-如何使包含小部件的框扩展为填充窗口?

[英]gtkmm - How to make box containing widgets expand to fill window?

I'm new to gtkmm and I'm a bit confused about packing and how to make widgets expand. 我是gtkmm的新手,我对打包以及如何使小部件扩展感到困惑。

This is the code I have written: 这是我编写的代码:

#include <gtkmm.h>

class GUIWindow : public Gtk::Window
{

    public:

    GUIWindow()
    {
        set_title("title");
        set_border_width(10);
        set_size_request(800, 600);

        add(_box_);

        _box_.pack_start(_grid_, true, true, 10);

        _grid_.add(_frame1_);
        _grid_.add(_frame2_);
        _grid_.attach_next_to(_frame3_, _frame1_, Gtk::POS_BOTTOM, 2, 1);

        _frame1_.set_label("f1");
        _frame2_.set_label("f2");
        _frame3_.set_label("f3");


        _label1_.set_text("Hello world");
        _label2_.set_text("this is an example GUI");
        _label3_.set_text("some example text");

        _frame1_.add(_label1_);
        _frame2_.add(_label2_);
        _frame3_.add(_label3_);

        show_all_children();

    }

    private:

    Gtk::Box _box_;

    Gtk::Grid _grid_;
    Gtk::Frame _frame1_;
    Gtk::Frame _frame2_;
    Gtk::Frame _frame3_;

    Gtk::Label _label1_;
    Gtk::Label _label2_;
    Gtk::Label _label3_;

};

int main(int argc, char *argv[])
{

    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");

    GUIWindow guiwindow;
    return app->run(guiwindow);

}

I wanted to add 3 labels, each inside their own frame. 我想添加3个标签,每个标签都在自己的框架内。 (Using the frame to see what is going on with the packing.) (使用框架查看包装的情况。)

I initially added a grid to my window and filled it with the 3 frames, filling each of those with a label. 最初,我在窗口中添加了一个网格,并在其中填充了3个框架,并在每个框架中都添加了标签。 However, the grid did not expand to fill the window. 但是,网格没有展开以填充窗口。 So I tried putting the grid inside a box, but the box also does not expand to fill the window. 因此,我尝试将网格放入框内,但是框也不会扩展以填充窗口。

I've searched the documentation and the web for a solution, but didn't find one. 我已经在文档和网络中搜索了一种解决方案,但没有找到解决方案。 I think I've just not quite understood how the packing works. 我想我只是不太了解包装的工作原理。

How can I get the " box " object to fill the entire window, so that when the window resizes the box also resizes, and the frames resize with the grid which resizes with the box. 我如何获得“ ”对象来填充整个窗口,以便在窗口调整大小时框也随之调整大小,框架的尺寸也随着网格随框的大小而变化。 (The labels presumably will not resize.) (标签可能不会调整大小。)

This is probably what you are looking for 这可能是您要寻找的

An example would be: 一个例子是:

for (const auto &child : _grid_.get_children()) {
    child->set_hexpand(true);
    child->set_halign(Gtk::ALIGN_FILL);
    child->set_vexpand(true);
    child->set_valign(Gtk::ALIGN_FILL);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM