Page 1 of 1

Writing text to a wxRichTextControl

Posted: Thu Feb 11, 2016 12:55 pm
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!

Re: Writing text to a wxRichTextControl

Posted: Thu Feb 11, 2016 4:46 pm
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

Re: Writing text to a wxRichTextControl

Posted: Sat Feb 13, 2016 8:10 pm
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?

Re: Writing text to a wxRichTextControl

Posted: Sun Feb 14, 2016 10:36 pm
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

Re: Writing text to a wxRichTextControl

Posted: Mon Feb 15, 2016 5:59 am
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.

Re: Writing text to a wxRichTextControl

Posted: Mon Feb 15, 2016 12:46 pm
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

Re: Writing text to a wxRichTextControl

Posted: Thu Feb 18, 2016 7:52 am
by imalipusram
Thanks Eran, for pointing me to my mistake.

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