Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

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
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

Hi,

I have created a working custom event with the help of the wiki.
Now I am trying the following:
I have 3 sets of header and source. A GUI set, Main app frame set, and a derived frame set.
What I am trying to do is trigger a custom event in the derived frame,
and have it picked up in the parent (Main frame) in order to execute a function there.

I am working on Windows 10 (latest version), Code::Blocks 20.03, mingw with gcc 11.2.0, and FrameBuilder.

The GUI has 2 classes: GUIframe and MyFrame2.

Code: Select all

GUIFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Frame 1"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 481,466 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
MyFrame2( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Frame 2"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,300 ), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );

The Main Frame looks as follows:
It has 1 button to trigger the custom event, and a function that responds to the event by showing text in a staticTextCtrl.

Code: Select all

enum
{
    evtCustom_1 = 1000
};

wxDEFINE_EVENT(EVT_CUSTOM_1, wxCommandEvent);
wxCommandEvent evt_1(EVT_CUSTOM_1, evtCustom_1);

Fun2Test3Frame::Fun2Test3Frame(wxFrame *frame) : GUIFrame(frame)
{
    Bind(EVT_CUSTOM_1, &Fun2Test3Frame::TestEvent1, this, evtCustom_1);
}

void Fun2Test3Frame::m_button2OnButtonClick( wxCommandEvent& event )
{
    evt_1.SetString("Triggered from Frame 1");
    wxPostEvent(this, evt_1);
}

void Fun2Test3Frame::TestEvent1(wxCommandEvent &event)
{
    m_staticText2->SetLabelText(event.GetString());
}
This works like a charm (thanks to the wiki)

The derived frame looks as follows:
It also has 1 button to trigger the custom event, and a function that responds to the event by showing text in a staticTextCtrl.

Code: Select all

enum
{
    evtCustom_2 = 1000
};

wxDEFINE_EVENT(EVT_CUSTOM_2, wxCommandEvent);
wxCommandEvent evt_2(EVT_CUSTOM_2, evtCustom_2);

Frame2::Frame2( wxWindow* parent ) : MyFrame2( parent )
{
    Bind(EVT_CUSTOM_2, &Frame2::TestEvent2, this, evtCustom_2);
}

void Frame2::m_button3OnButtonClick( wxCommandEvent& event )
{
    evt_2.SetString("Triggered From Frame 2");
    wxPostEvent(this, evt_2);

    wxWindow *prnt = GetParent();
    if(prnt != NULL)
    {
        wxPostEvent(prnt, evt_2);
//        GetParent()->GetEventHandler()->ProcessEvent(evt_2);
    }
}

void Frame2::TestEvent2(wxCommandEvent &event)
{
    m_staticText3->SetLabelText("Frame2 TestEvent2 triggered");
}
In the derived Frame, wxPostEvent(this, evt_2) works as planned.
I cannot make the second one work (catch the event and trigger execution of TestEvent1 in the Main Frame).

I gave evtCustom_1 and evtCustom_2 the same id, thinking that the id_name does not matter, but its value does.
Giving it different values made no difference.
The event is triggered by the value, not the name right?
prnt->GetLabel() returns the title of the Main frame ("Frame 1"), so this looks good.

According to the wiki (and several posts I found), this should work.
Have I missed something important here?

I appreciate your help to make it work. Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by doublemax »

I'm not 100% sure as i can't test it, but i have an idea:

Code: Select all

void Frame2::m_button3OnButtonClick( wxCommandEvent& event )
{
    evt_2.SetString("Triggered From Frame 2");
    wxPostEvent(this, evt_2);

    wxWindow *prnt = GetParent();
    if(prnt != NULL)
    {
        wxPostEvent(prnt, evt_2);
//        GetParent()->GetEventHandler()->ProcessEvent(evt_2);
    }
}
If you really have the event evt_2 as global variable (which you shouldn't), it's possible that it doesn't trigger in the main frame because it's already marked as "handled". For a test, try calling evt_2.Skip() before re-sending it. If it works, you probably don't have to re-send it at all, it will propagate to the parent anyway.

But you should really create the event on the stack at runtime.
Use the source, Luke!
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

I am not sure what you mean with creating the event on the stack at runtime. How is this done?
evt_2.Skip() did not help, and removing the 1st call did not either
I have moved all event stuff in the OnButtonClick function

Code: Select all

void Frame2::m_button3OnButtonClick( wxCommandEvent& event )
{
    wxWindow *prnt = GetParent();
    if(prnt != NULL)
    {
        wxDEFINE_EVENT(EVT_CUSTOM_2, wxCommandEvent);
        wxCommandEvent evt_2(EVT_CUSTOM_2, evtCustom_2);
        Bind( EVT_CUSTOM_2, &Frame2::TestEvent2, this, evtCustom_2);
        evt_2.Skip();
        evt_2.SetString("Triggered From Frame 2 as well");
        wxPostEvent(prnt, evt_2);
    }
}
This did not help either
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

I tried to add a small .rar file (3036KB) but received a HTTP error.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by doublemax »

RuudH wrote: Tue Jan 25, 2022 12:34 pm I tried to add a small .rar file (3036KB) but received a HTTP error.
That's far too much anyway. And i don't use CodeBlocks either. Try to clean it up and post just the source files.
Use the source, Luke!
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

the header and source files are in the .rar
Attachments
Fun2Test3.rar
(4.67 KiB) Downloaded 31 times
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by doublemax »

In Frame2 you send an event of type EVT_CUSTOM_2, but in Fun2Test3Frame you only Bind an event handler for EVT_CUSTOM_1.

In addition to the wxDEFINE_EVENT() lines in the .cpp files, you also need an equivalent wxDECLARE_EVENT in the header files. That way you can make the event type EVT_CUSTOM_2 known to other classes without redefining it.

There were also a few mistakes with regards to window destruction which lead to crashes when closing the app.
Attachments
_minimal_frame_event_propagation.7z
(2.87 KiB) Downloaded 34 times
Use the source, Luke!
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

Thank you.
Just one thing I do not understand.
I changed everything as shown in your attachment.

None of the events are triggered now
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by doublemax »

Did you carry over the changes manually to another project? Then you must have forgotten something. In my test project with these exact six files, the events work.
Use the source, Luke!
RuudH
Earned a small fee
Earned a small fee
Posts: 24
Joined: Thu Jun 04, 2020 6:35 am

Re: Custom event posted in derived frame(class) file does not trigger in parent frame (class) file

Post by RuudH »

Sorry, Sorry, Sorry :-(
My stupid mistake.

Thank you so much. Its working.
And, more important, I begin to understand the principles (and that was what this exercise was all about).
Post Reply