Writing text to a wxRichTextControl

Post here wxCrafter related issues / features / bugs
imalipusram
CodeLite Enthusiast
Posts: 16
Joined: Sat Sep 04, 2010 2:02 am
Genuine User: Yes
IDE Question: C++
Location: DE
Contact:

Writing text to a wxRichTextControl

Post by imalipusram »

Dear Experts,

I used wxCrafter to create a GUI with a wxRichTextControl. wxCrafter has produced the usual files (wxcrafter.h and .cpp, MainFrame.h and .cpp).

The control is named m_RichTextCtrl_1 and I would like to write some text to it from another class. I tried like suggested at http://docs.wxwidgets.org/trunk/overvie ... tctrl.html (see snippet below), with the result that I cannot access m_RichTextCtrl_1 as it is protected. However, I am not supposed to edit the wxcrafter.* files, of course, in order to change that.

Code: Select all

wxRichTextCtrl& r =  MainFrame::m_richTextCtrl_1; 
    r.AppendText("This is the message!");


What am I missing? (Probably a lot, please be patient with a newbie to C++, OOP and wxCrafter).

Thanks for your help!
User avatar
eranif
CodeLite Plugin
Posts: 6375
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: Writing text to a wxRichTextControl

Post by eranif »

imalipusram wrote:the result that I cannot access m_RichTextCtrl_1 as it is protected
You are not supposed to access it directly.

However, wxCrafter generates 'Get' methods to all of the GUI widgets in the base class .In your case, it should generate a function name 'GetRichTextCtrl1()'

Also, you can create a 'Get' function from the subclass.
wxRichTextCtrl& r = MainFrame::m_richTextCtrl_1;
r.AppendText("This is the message!");
Does this code compile?? m_richTextCtrl_1 is a pointer and you are assigning it into a reference...


Eran
Make sure you have read the HOW TO POST thread
imalipusram
CodeLite Enthusiast
Posts: 16
Joined: Sat Sep 04, 2010 2:02 am
Genuine User: Yes
IDE Question: C++
Location: DE
Contact:

Re: Writing text to a wxRichTextControl

Post by imalipusram »

Ok, so I replaced the code:

Code: Select all

GetRichTextCtrl_1()->AppendText("This is the message!\n\r");
In the corresponding header file, I declare:

Code: Select all

#include "wxcrafter.h"
#include <wx/richtext/richtextctrl.h> 
wxRichTextCtrl* GetRichTextCtrl_1();
wxcrafter.h is providing as public:

Code: Select all

wxRichTextCtrl* GetRichTextCtrl_1() { return m_richTextCtrl_1; }
It compiles, but now I get a linker error:
undefined reference to GetRichTextCtrl_1();
Edit: Linker options are:
$(shell wx-config --libs std,richtext --debug); -lboost_system; -lboost_thread
Where is my mistake?
User avatar
eranif
CodeLite Plugin
Posts: 6375
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: Writing text to a wxRichTextControl

Post by eranif »

Ok, so I replaced the code:
Where are you calling this code from? Please provide some more context
In the corresponding header file, I declare:
Why? wxcrafter.h already had this declared, you don't need to add it

The link error is because you defined it again.

Eran
Make sure you have read the HOW TO POST thread
imalipusram
CodeLite Enthusiast
Posts: 16
Joined: Sat Sep 04, 2010 2:02 am
Genuine User: Yes
IDE Question: C++
Location: DE
Contact:

Re: Writing text to a wxRichTextControl

Post by imalipusram »

Thanks for helping me, Eran!


c_myclass.cpp

Code: Select all

#include "c_myclass.h"

c_myclass::c_myclass()
{
}
c_myclass::~c_myclass()
{
}

void c_myclass::test_richtextctrl() // for testing acccess of controls
    {
    GetRichTextCtrl_1()->AppendText("This is the message!\n\r");
    }
c_myclass.h

Code: Select all

#include "wxcrafter.h"
#include <wx/richtext/richtextctrl.h> 

wxRichTextCtrl* GetRichTextCtrl_1();

class c_myclass
{
public:    
	c_myclass();
	~c_myclass();

void test_richtextctrl(void);
}
when I remove the line, "wxRichTextCtrl* GetRichTextCtrl_1();", the compiler complains that
in c_myclass.cpp, GetRichTextCtrl_1(); was not declared in this scope.
User avatar
eranif
CodeLite Plugin
Posts: 6375
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: Writing text to a wxRichTextControl

Post by eranif »

Obviously this will not work... you need an access to window holding the rich text control...
This falls into general C++ programming and this is not the correct forum to ask such questions

In general, you need 'c_myclass' to have an access to the actual window that holds the wxRichTextCtrl
so the frame class that holds the wxRichTextCtrl is kept as member in 'c_myclass' or it should subclass the frame class

Eran
Make sure you have read the HOW TO POST thread
imalipusram
CodeLite Enthusiast
Posts: 16
Joined: Sat Sep 04, 2010 2:02 am
Genuine User: Yes
IDE Question: C++
Location: DE
Contact:

Re: Writing text to a wxRichTextControl

Post by imalipusram »

Thanks Eran, for pointing me to my mistake.

It's sometime just confusing to draw the right conclusions from compiler error messages.
Post Reply