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.
Marco
Experienced Solver
Posts: 65 Joined: Wed Aug 17, 2005 2:25 pm
Location: Rennes, France
Post
by Marco » Tue Apr 18, 2006 8:47 am
Hi everybody,
I try to test wxCalendarCtrl with a small application but this doesn't work at all.
Code: Select all
#include "MainFrame.hpp"
#include "wx/wx.h"
#include "wx/calctrl.h"
#include "wx/datetime.h"
typedef enum wxwidgets__ids
{
ID_PANEL,
ID_CALCTRL
} wxwidgets__id_t;
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_CALENDAR_SEL_CHANGED(ID_CALCTRL, MainFrame::OnDateSelectedChange)
EVT_CLOSE(MainFrame::OnClose)
END_EVENT_TABLE()
MainFrame::MainFrame()
:wxFrame (NULL, -1, "Calendar")
{
wxPanel* p_main_panel = new wxPanel(this, ID_PANEL);
wxBoxSizer* p_main_box_sizer = new wxBoxSizer(wxVERTICAL);
p_main_panel->SetSizer(p_main_box_sizer);
wxCalendarCtrl* p_calendar = new wxCalendarCtrl(p_main_panel, ID_CALCTRL, wxDateTime());
p_main_box_sizer->Add(p_calendar, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
wxStaticText* p_static_text = new wxStaticText(p_main_panel, wxID_STATIC, _(p_calendar->GetDate().FormatISODate()));
p_main_box_sizer->Add(p_static_text, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxADJUST_MINSIZE, 5);
}
void MainFrame::OnDateSelectedChange(wxCalendarEvent& event)
{
// -> Crash here ...
p_static_text->SetLabel(p_calendar->GetDate().FormatISODate());
}
void MainFrame::OnClose (wxCloseEvent& WXUNUSED(event))
{
delete p_static_text;
delete p_calendar;
delete p_main_box_sizer;
delete p_main_panel;
//free memory
this->Destroy ();
}
I don't know why but when I click on a new date, the application crashes ...
Could you help me please ?
Marco
benedicte
wxWorld Domination!
Posts: 1409 Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France
Post
by benedicte » Tue Apr 18, 2006 8:52 am
Did you try to get the date directly from the event?
Code: Select all
void MainFrame::OnDateSelectedChange(wxCalendarEvent& event)
{
p_static_text->SetLabel(event.GetDate().FormatISODate());
}
Also try to use local variables to determine which call makes your app crash: GetDate(), FormatISODate(), SetLabel()...
phlox81
wxWorld Domination!
Posts: 1387 Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:
Post
by phlox81 » Tue Apr 18, 2006 8:54 am
you initialize local variables in your constructor,
they may overlay membervariables...
So p_static_text is a unvalid pointer in you EventHandler.
Marco
Experienced Solver
Posts: 65 Joined: Wed Aug 17, 2005 2:25 pm
Location: Rennes, France
Post
by Marco » Tue Apr 18, 2006 9:08 am
Thank you benedicte and phlox81 for your responses,
phlox81 wrote: you initialize local variables in your constructor,
they may overlay membervariables...
So p_static_text is a unvalid pointer in you EventHandler.
I apologize !!! Thank you very much ...
But now I have another problem on line:
I can delete other components but I have a crash when I try to delete this wxBoxSizer ... Why ? Can someone help me please ?
Thank you in advance again,
Marco
phlox81
wxWorld Domination!
Posts: 1387 Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:
Post
by phlox81 » Tue Apr 18, 2006 9:25 am
wxWidgets does the deletion of all childwindows and sizers by it self.
Actually when ever you hand over a Pointer to wxWidgets,
it mostly will handle its deletion. If you want to delete a pointer
your self, that is of wxWidgets (Window,sizer etc.) itself,
than call Destroy instead.
phlox