sizer problems Topic is solved

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
milkies
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Oct 20, 2010 12:27 pm

sizer problems

Post by milkies »

Hi,

I am new to wxWidgets and reading the documentation i thoguht id tackle these sizer objects head on as it seems one of the most complicated areas to first get your head around.

I believe I understand them and tried to implement a dialog with 1 list box and two buttons. However when i run it everything is squashed into the top left corner. As soon as i stretch the frame (even by 1 pixel) everything suddenly jumps to the correct position and behaves correctly. It is just first load that is broke. Can someone take a look please?

Thanks

Code: Select all


MainApp.cpp /////////////////////////////////////////

#include "MainApp.h"
#include "MainDlg.h"

IMPLEMENT_APP(MainApp)

MainApp::~MainApp(void)
{
}

bool MainApp::OnInit()
{
	MainDlg *frame = new MainDlg( _("MainApp"), wxPoint(-1, -1), wxSize(450, 340) );
	frame->Show(true);
	SetTopWindow(frame);
	return true;
}

MainApp.h /////////////////////////////////////

class MainApp : public wxApp
{
public:
	virtual bool OnInit();
	virtual ~MainApp( ) ;
};

MainDlg.h //////////////////////////////////////

#include "wx/wx.h"

class MainDlg: public wxFrame
{
public:

	MainDlg(const wxString& title, const wxPoint& pos, const wxSize& size);

	void OnQuit(wxCommandEvent& event);
	void OnListDblClick(wxCommandEvent& event);
	void OnAdd(wxCommandEvent& event);

	DECLARE_EVENT_TABLE()

	enum
	{
		ID_LIST,
		ID_ADD,
		ID_CLOSE
	};

private:
	void CreateListBox( wxPanel* parent, wxSizer* sizer ) ;
	void CreateButtons( wxPanel* parent, wxSizer* sizer ) ;
	
	wxListBox* m_wxList ;
	wxButton* m_wxBtnClose ;
	wxButton* m_wxBtnAdd ;
};

MainDlg.cpp ///////////////////////////////////////////////

#include "Config.h"
#include "MainDlg.h"

BEGIN_EVENT_TABLE(MainDlg, wxFrame)
	EVT_MENU(wxID_EXIT,  MainDlg::OnQuit)
	EVT_LISTBOX_DCLICK(MainDlg::ID_LIST,  MainDlg::OnListDblClick)
	EVT_BUTTON(MainDlg::ID_CLOSE,  MainDlg::OnQuit)
	EVT_BUTTON(MainDlg::ID_ADD,  MainDlg::OnAdd)
END_EVENT_TABLE()

MainDlg::MainDlg(const wxString& title, const wxPoint& pos, const wxSize& size)
	: wxFrame(NULL, -1, title, pos, size)
	, m_wxList(NULL)
{
	wxPanel* panel = new wxPanel(this, -1) ;
	wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL) ;
	CreateListBox( panel, vbox ) ;
	CreateButtons( panel, vbox ) ;	
	panel->SetSizer(vbox) ;
	Center( ) ;
}

void MainDlg::CreateListBox( wxPanel* parent, wxSizer* sizer )
{
	m_wxList = new wxListBox(parent, ID_LIST, 
		wxPoint(-1, -1), wxSize(-1, -1)) ; 

	sizer->Add(m_wxList, 15, wxEXPAND | wxALL, 20) ;
}

void MainDlg::CreateButtons( wxPanel* parent, wxSizer* sizer )
{
	wxPanel* panel = new wxPanel(parent, -1);
	wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
	m_wxBtnClose = new wxButton(panel, ID_CLOSE, wxT("Quit"));
	m_wxBtnAdd = new wxButton(panel, ID_ADD, wxT("Add"));	
	hbox->Add(m_wxBtnAdd,1);
	hbox->Add(m_wxBtnClose,1);
	panel->SetSizer(hbox);
	sizer->Add(panel, 3, wxCENTER, 20);
}

void MainDlg::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(true);
}

void MainDlg::OnListDblClick(wxCommandEvent& WXUNUSED(event))
{
// TODO
}


void MainDlg::OnAdd(wxCommandEvent& WXUNUSED(event))
{
// TODO
}

[/code]
zaphod
Earned a small fee
Earned a small fee
Posts: 23
Joined: Tue Jun 05, 2007 1:58 pm
Location: Germany

Post by zaphod »

Try

Code: Select all

GetSizer()->SetSizeHints(this);
just before the Center() call in your MainDlg constructor. This will layout your controls and adjust the dialog size to the space needed by the controls.

BTW, you do not need to create those wxPanels. Sizers make up their own layout hierarchy, you do not need a window hierarchy matching the sizers.

I would also recommend that you try one of the dialog editors for wxWidgets, e.g. wxFormBuilder. It is easy to setup sizers there, and you can look at generated C++ code to get some understanding for handcrafted code.
milkies
In need of some credit
In need of some credit
Posts: 3
Joined: Wed Oct 20, 2010 12:27 pm

Post by milkies »

thanks for your help. That worked great :)
Post Reply