Page 1 of 1

Subclass wxTextCtr

Posted: Mon Apr 06, 2020 6:21 am
by Dust Commander
This is probably a newbie c++ issue. I am trying to use a subclass on a wxTextCtrl.

The text control appears correctly without a subclass. It does not appear at all when using the Subclass.

Steps to modify the functioning code without the Subclass:

1. Enter messagePane for the Subclass Class Name
2. Enter Message Frame.h for the include file
3. Hit the gear to Generate C++ and XRC code
4. File>Generate Code
5. Build -- no build warnings or errors

MessageFrame.h contents:
___________________________________________________________________
class messagePane : public wxTextCtrl
{
public:
messagePane(wxWindow *parent,
wxWindowID id,
const wxString &value = wxEmptyString,
const wxPoint &pos = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
long style = 0,
const wxValidator& validator = wxDefaultValidator,
const wxString &name = wxTextCtrlNameStr);
};
___________________________________________________________________


MessageFrame.cpp contents:

___________________________________________________________________
#include <wx/wx.h>
#include "MessageFrame.h"

messagePane ::messagePane(wxWindow *parent,
wxWindowID id,
const wxString &value,
const wxPoint &pos,
const wxSize &size,
long style,
const wxValidator& validator,
const wxString &name)
{
wxTextCtrl(parent,
id,
value,
pos,
size,
style,
validator,
name);

};

___________________________________________________________________

Re: Subclass wxTextCtr

Posted: Mon Apr 06, 2020 1:30 pm
by DavidGH
Hi,

If the code you posted is accurate, your messagePane::messagePane is wrong: you are creating a wxTextCtrl on the stack, so it immediately loses scope when the constructor ends.

You should be using an initializer list, the bit after the colon below; see e.g. https://www.geeksforgeeks.org/when-do-w ... list-in-c/

Code: Select all

messagePane ::messagePane(wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize size, long style, const wxValidator& validator, const wxString &name)
: wxTextCtrl(parent, id, value, pos, size, style, validator, name)
{
	// Do interesting things here...
}
Did you try running the wxCrafter 'Preview'? I think you'll find that messagePane displays OK there; which makes it much less likely to be a wxCrafter issue.

Regards,

David

Re: Subclass wxTextCtr

Posted: Mon Apr 06, 2020 3:09 pm
by Dust Commander
Hi David,

Thank you for the reply. The code listed is an exact copy of the code in my project.

I think the initializer list is in the header file declaration. I had the initializers in the function body and got compiler errors. I thing this tells me how to create a constructor for the subclass.

It runs when I code it like this:

messagePane ::messagePane(wxWindow *parent,
wxWindowID id,
const wxString &value,
const wxPoint &pos,
const wxSize &size,
long style,
const wxValidator& validator,
const wxString &name)
: wxTextCtrl(parent,
id,
value,
pos,
size,
style,
validator,
name)
{

};

I will have to study why this works.

Best regards,
Stev

Re: Subclass wxTextCtr

Posted: Mon Apr 06, 2020 4:42 pm
by DavidGH
I think the initializer list is in the header file declaration
Not in the code that you posted. And it shouldn't have been because you defined the constructor in the cpp file, so that's where the initializer list has to be.