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);
};
___________________________________________________________________
Subclass wxTextCtr
-
- CodeLite Curious
- Posts: 3
- Joined: Mon Apr 06, 2020 5:58 am
- Genuine User: Yes
- IDE Question: C++
- Contact:
-
- CodeLite Plugin
- Posts: 819
- Joined: Wed Sep 03, 2008 7:26 pm
- Contact:
Re: Subclass wxTextCtr
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/
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
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...
}
Regards,
David
-
- CodeLite Curious
- Posts: 3
- Joined: Mon Apr 06, 2020 5:58 am
- Genuine User: Yes
- IDE Question: C++
- Contact:
Re: Subclass wxTextCtr
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
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
-
- CodeLite Plugin
- Posts: 819
- Joined: Wed Sep 03, 2008 7:26 pm
- Contact:
Re: Subclass wxTextCtr
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.I think the initializer list is in the header file declaration