简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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