I just can't understand sizers - trying to make panel fill horizontal space

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wonkey_monkey
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sun May 26, 2019 8:10 pm

I just can't understand sizers - trying to make panel fill horizontal space

Post by wonkey_monkey »

I'm normally pretty good at being able to find answers via Google, but when it comes to understanding sizers I'm hitting a brick wall. I've read every tutorial and guide on sizers that I can find but I'm either overlooking something or there's something missing from them.

What I've got so far is this:

Image

It's hard to see but there is 1px wide red panel to the left of the green panel.

My main Frame (yellow) has a vertical BoxSizer, to which I add a text control, a horizontal BoxSizer containing two panels (red and green), and another text control:

Code: Select all

	wxBoxSizer* hSizer = new wxBoxSizer(wxHORIZONTAL);
	wxPanel *panel1 = new wxPanel(this, wxID_ANY);
	panel1->SetBackgroundColour(*wxRED);
	wxPanel *panel2 = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(128, 128));
	panel2->SetBackgroundColour(*wxGREEN);
	hSizer->Add(panel1, 1, wxEXPAND);
	hSizer->Add(panel2, 0, wxEXPAND);

	wxBoxSizer* vSizer = new wxBoxSizer(wxVERTICAL);
	vSizer->Add(new wxTextCtrl(this, wxID_ANY));
	vSizer->Add(hSizer, 1);
	vSizer->Add(new wxTextCtrl(this, wxID_ANY));

	SetSizer(vSizer);

	SetBackgroundColour(*wxYELLOW);
What I want is for the red panel to fill all the horizontal space, except for the space taken up by the green panel. I can almost achieve this by giving the red panel a proportion of 1000 and the green panel a proportion of 1, but that's a bit of a kludge, and it won't work if I hide the green panel.

I don't like asking such a specific question because I'd prefer to understand sizers as a whole rather than get an answer to my specific problem, but I feel like I'm stuck in a loop where I don't understand sizers well enough to ask the right question to help me understand sizers!

Is there some concept I'm missing here? Is wxPanel the wrong thing to use? Does hSizer need something doing to it to make its children fill its whole width?
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: I just can't understand sizers - trying to make panel fill horizontal space

Post by Kvaz1r »

In such cases usually much easier use some RAD tool like wxFormBuilder.

As for here replace

Code: Select all

vSizer->Add(hSizer, 1);
with

Code: Select all

vSizer->Add(hSizer, 1,wxEXPAND);
wonkey_monkey
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sun May 26, 2019 8:10 pm

Re: I just can't understand sizers - trying to make panel fill horizontal space

Post by wonkey_monkey »

Ah, I just figured that out one minute after you posted, I swear! Not that I exactly get how it works, but it works.

I'll look into wxFormBuilder, too, although this is going to be a pretty simple program and I had enough problems just trying to use a PNG resource...

Thanks!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: I just can't understand sizers - trying to make panel fill horizontal space

Post by doublemax »

I disagree about using a GUI editor in this case, because if you don't know how sizers work in principle, you won't get any further than trial-and-error either. The trial-and-error will just be a little faster :)

You say you've read all sizer tutorials, so i assume you found this one:
http://neume.sourceforge.net/sizerdemo/
Use the source, Luke!
wonkey_monkey
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sun May 26, 2019 8:10 pm

Re: I just can't understand sizers - trying to make panel fill horizontal space

Post by wonkey_monkey »

Yes I did, thanks. I think I just hadn't made the conceptual connection that sizers could/should wxEXPAND as well.

I'm finding wxFormBuilder useful for generating C++ code which I can then pore over and understand, rather than just letting it do the work for me.
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: I just can't understand sizers - trying to make panel fill horizontal space

Post by alys666 »

there is quite easy tutorial for wx sizers, but in python notation. :)
https://wxpython.org/Phoenix/docs/html/ ... rview.html
ubuntu 20.04, wxWidgets 3.2.1
Post Reply