showing event of child control from parent 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
NoeMurr
Knows some wx things
Knows some wx things
Posts: 32
Joined: Sat Mar 31, 2018 2:26 pm

showing event of child control from parent

Post by NoeMurr »

Hi all,

I have a custom control class, that is derived from wxControl and that is composed by some other standard control (I can't post the original code becouse it is too long and complex so, I will write an example):

Code: Select all

...
class MyControl : public wxControl {
	wxTextCtrl *text;
	wxButton *btn;
	
public:
	MyControl(...) : wxControl(...) {
		auto *sizer = new wxBoxSizer(wxVERTICAL);
		btn = new wxButton(this, wxID_ANY, _T("btn"));
		text = new wxTextCtrl(...);
		sizer->Add(text);
		sizer->Add(btn);
		
		SetMainSizer(sizer);
		
		btn->Bind(wxEVT_BUTTON, &MyControl::onBtnClick, this);
	}
	
	void onBtnClick(wxCommandEvent &e) {
		// todo
	}
}
...
In the todo section I want to fire a custom event that can be catched from outside the control e.g.:

Code: Select all

...
	MyControl *myCtrl = new MyControl(...);
	myCtrl->Bind(MY_EVT, [](MyCoolEvt &e){
		std::cout << e->GetMyData() << std::endl;
	});
...
Does someone know how should I do it?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7481
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: showing event of child control from parent

Post by ONEEYEMAN »

Hi,
Did you look at the event sample?

Thank you.
NoeMurr
Knows some wx things
Knows some wx things
Posts: 32
Joined: Sat Mar 31, 2018 2:26 pm

Re: showing event of child control from parent

Post by NoeMurr »

I did, it is very complex but I think I found the way.. Can you tell me if I'm wrong at some point?

1) I should create an event class (that inherits from wxEvent);
2) I should decleare the event with:

Code: Select all

wxDEFINE_EVENT(MY_EVT, MyEvtClass);
3) in my todo section I should create the event and fire it using ProcessWindowEvent() function:

Code: Select all

...
    MyEvtClass evt(MY_EVT, GetId());
    evt.SetEventObject(this);
    ProcessWindowEvent(event);
...
4) when I need to handle the event I should do something like:

Code: Select all

...
MyControl *myCtrl = new MyControl(...);
myCtrl->Bind(MY_EVT, [](MyEvtClass &e){
      	std::cout << "Yeah! that's my event" << std::endl;
});
...
User avatar
doublemax
Moderator
Moderator
Posts: 19162
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: showing event of child control from parent

Post by doublemax »

You only need to define a new event class when you need to transport more information than an integer and a string. Otherwise just defining a new event type is enough.

But if you define a new class, you must derive from wxCommandEvent. Only these propagate upwards in the window hierarchy.

Code: Select all

MyEvtClass evt(MY_EVT, GetId());
This is a common mistake that will not cause a compiler error, but lots of grey hair. The first parameter is the id, then the event type.
Use the source, Luke!
NoeMurr
Knows some wx things
Knows some wx things
Posts: 32
Joined: Sat Mar 31, 2018 2:26 pm

Re: showing event of child control from parent

Post by NoeMurr »

Thank you! I have to transport a custom data so I need the class.
Post Reply