How to set up a panel/dialog as child for a frame with wxFormBuilder Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
kagi3624
Knows some wx things
Knows some wx things
Posts: 29
Joined: Wed Feb 26, 2020 8:13 am

How to set up a panel/dialog as child for a frame with wxFormBuilder

Post by kagi3624 »

Hello, I am a little bit confused how to work with wxFormBuilder and to create child panels and dialogs for the frame. In tutorials like this a top sizer is created and then later the panel is attached to it with

Code: Select all

panel->SetSizer(topSizer);
When I create a panel in wxFormBuilder I don't see how I can attach it to any sizer. Any dialog, frame or panel is always created at the same level as the main frame:
wx.png
So a class is created in Guiframe.h and Guiframe.cpp, that I am not allowed to touch, like that:

Code: Select all

class MyPanel1 : public wxPanel
{
	private:

	protected:
		wxButton* m_button4;
		wxButton* m_button5;
		wxButton* m_button6;

		// Virtual event handlers, override them in your derived class
		virtual void m_button4OnButtonClick( wxCommandEvent& event ) { event.Skip(); }


	public:

		MyPanel1( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 292,60 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );

		~MyPanel1();

};
And since it tells me to override the event handlers in my derived class I create my own derived class:

Code: Select all

class pnl1 : public MyPanel1{

    public:
        using MyPanel1::MyPanel1;
    private:
        virtual void m_button1OnButtonClick( wxCommandEvent& event ) { event.Skip(); }
};
I don't know how to create a constructor for this, so I steal it from the base class, also do I need a destructor? Anyway, I add

Code: Select all

pnl1 *firstp;
to the derived frame class that is automatically created by the code::blocks project wizzard and in its constructor I add

Code: Select all

firstp = new pnl1(this, wxID_ANY);
and I get my stretched panel in the frame.
window.png
window.png (13.09 KiB) Viewed 2785 times
The problem is, I have absolutely no clue what I did and why this worked since there is no top sizer to connect it with the frame...Can someone explain to me what is going on? Why can't I put a panel with wxFormBuilder in a sizer ?

Thanks!

I have wxWidgets 3.1.5, wxFormbuilder 3.10.0-4761b0c and code::blocks 20.03
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set up a panel/dialog as child for a frame with wxFormBuilder

Post by doublemax »

I don't use wxFormbuilder (or any GUI editor), but i can tell you why it worked:

Whenever a toplevel window (wxFrame, wxDialog) has exactly one immediate child, this child will always automatically be stretched to fill the whole client area.

In general you should be able to do this when starting with an empty project:
Create wxFrame
Select wxFrame, add wxPanel
Select wxPanel, add wxBoxSizer

This should create the structure you want.
Use the source, Luke!
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: How to set up a panel/dialog as child for a frame with wxFormBuilder

Post by New Pagodi »

To add a panel to a frame, you can use the panel on the containers tab.
1.png
1.png (13.47 KiB) Viewed 2752 times
It's different from the panel on the forms tab and can be added to a frame. Note that you'll first need to set a sizer for the frame. (I know wxWidgets lets you add a panel directly to the frame without a sizer, but wxFormbuilder requires every control to be placed in a sizer even if wxWidgets doesn't.)
2.png
2.png (29.68 KiB) Viewed 2752 times
You should also remove all the borders from the panel you set on the frame. Otherwise you'll have an ugly border around the panel on some platforms (notably windows).

Once you have the panel set, you can add a sizer to the panel and then add whatever controls you want to the sizer.
3.png
3.png (20.73 KiB) Viewed 2752 times
Post Reply