问题
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.)
回答1:
This is probably what you are looking for
- set_halign(Align align)
- set_valign(Align align)
- set_hexpand(bool expand = true)
- set_vexpand(bool expand = true)
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);
}
来源:https://stackoverflow.com/questions/49088760/gtkmm-how-to-make-box-containing-widgets-expand-to-fill-window