Page 1 of 1

Can't Add Widgets to wxDialog

Posted: Wed May 26, 2010 9:11 pm
by Deluge
I've always been able to add widgets to a custom dialog with wxPython, but I am having trouble doing it with wxWidgets in C++. According to Zetcode I should be able to do it.

Here is the member function that contains my dialog:

Code: Select all

void MainWindow::OnHelp(wxMenuEvent& event)
{
	wxDialog help(this, -1, _T("Help"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
	help.SetIcon(ICON1);
	
	wxTextCtrl textarea(help, -1, wxEmptyString);
	
	help.ShowModal();
	help.Destroy();
}
When I try to compile, this is the error that comes up:
In member function `void MainWindow::OnHelp(wxMenuEvent&)':
error: no matching function for call to `wxTextCtrl::wxTextCtrl
(wxDialog&, int, const wxChar*&)'
textctrl.h:278: note: candidates are: wxTextCtrl:
:wxTextCtrl(const wxTextCtrl&)
textctrl.h:29: note: wxTextCtrl::
wxTextCtrl(wxWindow*, wxWindowID, const wxString&, const wxPoint&, const wxSize&
, long int, const wxValidator&, const wxString&)
textctrl.h:21: note: wxTextCtrl::
wxTextCtrl()

Posted: Wed May 26, 2010 9:45 pm
by Tony0945
The dialog "help" is created on the stack. The TextCtrl constructor wants a pointer to a window and you are passing the whole object.

You want:

Code: Select all


wxTextCtrl textarea(&help, -1, wxEmptyString);
or

Code: Select all


  wxDialog *help = new wxDialog(this, -1, _T("Help"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
I think you have to explicity destroy the dialog in the second method. I prefer the first.

Posted: Wed May 26, 2010 9:49 pm
by Tony0945
I missed that you are explicitly destroying the dialog. You don't have to do that if it's on the stack. Don't know what happens if you try to delete memory that wasn't allocated with "new", but it can't be good.

The stack frame object will go away when the function returns.

Posted: Wed May 26, 2010 10:15 pm
by TrV
For this kind of stuff, it's easier to always create objects on the heap!
wxDialog object can be created on the heap as well as on the stack (but i do prefer on the heap).
wxTextCtrl and such kind of graphic objects are generally created on the heap as the parent (the dialog) will take care of destroying its children automatically.

Posted: Wed May 26, 2010 10:36 pm
by Deluge
Thank you both very much. I am still learning about the heap and stack.