layout problem

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
airc
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jan 06, 2014 7:35 am

layout problem

Post by airc » Wed Jul 01, 2020 7:07 am

Hi
i used to use wxformbuilder with wxwidgets 2.8 , and every things was fine until i come across with this situation where the layout inside wxformbuilder doesn't match the layout in my application
does this mean wxformbuilder generate wrong code ?
this is the code generated

Code: Select all

frame::frame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : 
	wxFrame( parent, id, title, pos, size, style )
{	
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );

	wxBoxSizer* bSizer1;
	bSizer1 = new wxBoxSizer( wxVERTICAL );

	m_panel1 = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
	wxBoxSizer* bSizer2;
	bSizer2 = new wxBoxSizer( wxVERTICAL );

	wxStaticBoxSizer* sbSizer1;
	sbSizer1 = new wxStaticBoxSizer( new wxStaticBox( m_panel1, wxID_ANY, wxT("label") ), wxHORIZONTAL );

	wxBoxSizer* bSizer3;
	bSizer3 = new wxBoxSizer( wxHORIZONTAL );

	m_staticText1 = new wxStaticText( sbSizer1->GetStaticBox(), wxID_ANY, wxT("MyLabel"), wxDefaultPosition, wxDefaultSize, 0 );
	m_staticText1->Wrap( -1 );
	bSizer3->Add( m_staticText1, 0, wxALL, 5 );

	wxArrayString m_choice1Choices;
	m_choice1 = new wxChoice( sbSizer1->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice1Choices, 0 );
	m_choice1->SetSelection( 0 );
	bSizer3->Add( m_choice1, 0, wxALL, 5 );


	sbSizer1->Add( bSizer3, 0, 0, 5 );


	bSizer2->Add( sbSizer1, 1, wxEXPAND, 5 );

	wxStaticBoxSizer* sbSizer2;
	sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( m_panel1, wxID_ANY, wxT("label") ), wxHORIZONTAL );

	wxBoxSizer* bSizer4;
	bSizer4 = new wxBoxSizer( wxHORIZONTAL );

	m_staticText5 = new wxStaticText( sbSizer2->GetStaticBox(), wxID_ANY, wxT("MyLabel"), wxDefaultPosition, wxDefaultSize, 0 );
	m_staticText5->Wrap( -1 );
	bSizer4->Add( m_staticText5, 0, wxALL|wxEXPAND, 5 );

	wxArrayString m_choice2Choices;
	m_choice2 = new wxChoice( sbSizer2->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice2Choices, 0 );
	m_choice2->SetSelection( 0 );
	bSizer4->Add( m_choice2, 0, wxALL, 5 );


	sbSizer2->Add( bSizer4, 1, wxEXPAND, 5 );


	bSizer2->Add( sbSizer2, 1, wxEXPAND, 5 );


	m_panel1->SetSizer( bSizer2 );
	m_panel1->Layout();
	bSizer2->Fit( m_panel1 );
	bSizer1->Add( m_panel1, 1, wxALL|wxEXPAND, 0 );


	this->SetSizer( bSizer1 );
	this->Layout();

	this->Centre( wxBOTH );
}
Attachments
wxFbuilder.png
wxFbuilder.png (54.59 KiB) Viewed 104 times
MyApp.png
MyApp.png (3.6 KiB) Viewed 104 times

catalin
Moderator
Moderator
Posts: 1574
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: layout problem

Post by catalin » Wed Jul 01, 2020 8:12 am

Remove `bSizer1`, `sbSizer1` and `sbSizer2` (not their contents) from your layout, they are useless anyway, in case you missed a relevant flag or proportion for them.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2429
Joined: Sun Jan 03, 2010 5:45 pm

Re: layout problem

Post by PB » Wed Jul 01, 2020 10:17 am

catalin wrote:
Wed Jul 01, 2020 8:12 am
Remove `bSizer1`
Annoyingly, wxFormBuilder requires a toplevel window to have a sizer, even if the window has just one child.

Anyway, the exact code works as expected with wxWidgets master on Windows 10:
twoboxes.png
twoboxes.png (4.38 KiB) Viewed 81 times
Full compilable code

Code: Select all

#include <wx/wx.h>

class MyFrame : public wxFrame
{
public:
    MyFrame () : wxFrame(nullptr, wxID_ANY, "Test")
    {
        this->SetSizeHints( wxDefaultSize, wxDefaultSize );

	    wxBoxSizer* bSizer1;
	    bSizer1 = new wxBoxSizer( wxVERTICAL );

	    m_panel1 = new wxPanel( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
	    wxBoxSizer* bSizer2;
	    bSizer2 = new wxBoxSizer( wxVERTICAL );

	    wxStaticBoxSizer* sbSizer1;
	    sbSizer1 = new wxStaticBoxSizer( new wxStaticBox( m_panel1, wxID_ANY, wxT("label") ), wxHORIZONTAL );

	    wxBoxSizer* bSizer3;
	    bSizer3 = new wxBoxSizer( wxHORIZONTAL );

	    m_staticText1 = new wxStaticText( sbSizer1->GetStaticBox(), wxID_ANY, wxT("MyLabel"), wxDefaultPosition, wxDefaultSize, 0 );
	    m_staticText1->Wrap( -1 );
	    bSizer3->Add( m_staticText1, 0, wxALL, 5 );

	    wxArrayString m_choice1Choices;
	    m_choice1 = new wxChoice( sbSizer1->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice1Choices, 0 );
	    m_choice1->SetSelection( 0 );
	    bSizer3->Add( m_choice1, 0, wxALL, 5 );


	    sbSizer1->Add( bSizer3, 0, 0, 5 );


	    bSizer2->Add( sbSizer1, 1, wxEXPAND, 5 );

	    wxStaticBoxSizer* sbSizer2;
	    sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( m_panel1, wxID_ANY, wxT("label") ), wxHORIZONTAL );

	    wxBoxSizer* bSizer4;
	    bSizer4 = new wxBoxSizer( wxHORIZONTAL );

	    m_staticText5 = new wxStaticText( sbSizer2->GetStaticBox(), wxID_ANY, wxT("MyLabel"), wxDefaultPosition, wxDefaultSize, 0 );
	    m_staticText5->Wrap( -1 );
	    bSizer4->Add( m_staticText5, 0, wxALL|wxEXPAND, 5 );

	    wxArrayString m_choice2Choices;
	    m_choice2 = new wxChoice( sbSizer2->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice2Choices, 0 );
	    m_choice2->SetSelection( 0 );
	    bSizer4->Add( m_choice2, 0, wxALL, 5 );


	    sbSizer2->Add( bSizer4, 1, wxEXPAND, 5 );


	    bSizer2->Add( sbSizer2, 1, wxEXPAND, 5 );


	    m_panel1->SetSizer( bSizer2 );
	    m_panel1->Layout();
	    bSizer2->Fit( m_panel1 );
	    bSizer1->Add( m_panel1, 1, wxALL|wxEXPAND, 0 );


	    this->SetSizer( bSizer1 );
	    this->Layout();

	    this->Centre( wxBOTH );
    }
private:
    wxPanel*      m_panel1;
    wxStaticText* m_staticText1;
    wxStaticText* m_staticText5;

    wxChoice*     m_choice1;
    wxChoice*     m_choice2;
}; 

class MyApp : public wxApp
{
public:   
    bool OnInit() override
    {
        (new MyFrame())->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);

But you are saying you stuck with wxWidgets 2.8? Based on the screenshot, it seems that your OS is also very old (or uses Win9x theme). Since wxWidgets v2.9.1, windows placed inside a wxStaticBoxSizer are supposed to be created with its GetStaticBox() as the parent as is done in your code, but I do not know if this works also in v2.8.

BTW, I would recommend human readable variable names even for the generated code. If nothing else, makes debugging much easier.

airc
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jan 06, 2014 7:35 am

Re: layout problem

Post by airc » Wed Jul 01, 2020 1:37 pm

thanks for the tips
am using win7 (with windows classic theme) , but as you mention maybe i should upgrade to wx3

Post Reply