Can't Add Widgets to wxDialog 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
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Can't Add Widgets to wxDialog

Post 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()
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Post 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.
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Post 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.
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post 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.
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Post by Deluge »

Thank you both very much. I am still learning about the heap and stack.
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
Post Reply