Can't connect to wxDialog's close event 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
denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Can't connect to wxDialog's close event

Post by denarced » Mon Jan 09, 2012 11:12 am

I'm running my code in a debugger and when the wxDialog in the following code is closed,
the event handler function winClosing is never reached. Any ideas why?

Code: Select all

bool MyApp::OnInit()
{
    wxFrame *frame = new wxFrame(NULL, wxID_ANY, "title", wxDefaultPosition, wxSize(250, 150));
    wxDialog *dialog = new wxDialog(frame, wxID_ANY, "Dialog title");
    Connect(dialog->GetId(), wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MyApp::winClosing));
    frame->Show(true);
    dialog->Show();
    return true;
}

void MyApp::winClosing(wxCloseEvent& event)
{
    event.GetEventObject();
}

User avatar
doublemax
Moderator
Moderator
Posts: 15264
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Can't connect to wxDialog's close event

Post by doublemax » Mon Jan 09, 2012 11:49 am

Only command events travel up in the window hierarchy, wxEVT_CLOSE_WINDOW does not.

If you really wanted to do something like this - which is very unusual - try this:

Code: Select all

dialog->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MyApp::winClosing), NULL, this);
The "NULL, this" at the end is important if you connect the method of one class (MyApp) to the event of another class (wxDialog).

However, wxDialog should be used for modal dialogs (and often created on the stack), wxFrame for non-modal dialogs.
Use the source, Luke!

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: Can't connect to wxDialog's close event

Post by denarced » Mon Jan 09, 2012 12:25 pm

doublemax wrote:Only command events travel up in the window hierarchy, wxEVT_CLOSE_WINDOW does not.
Let's see if I understood you correctly. The event is raised in the dialog itself. Trying to "register" the event to another object doesn't work because it doesn't travel up in the window hierarchy; in this case from the dialog to the MyApp-object (wxApp). The fix is to also define the event sink, which I assume, is the first place where the event is raised. And this is the reason why the following Bind-command also works (not directly tested):

Code: Select all

dialog->Bind(wxEVT_CLOSE_WINDOW, &MyApp::winClosing, this)
It too defines the event sink.

Correct?

User avatar
doublemax
Moderator
Moderator
Posts: 15264
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Can't connect to wxDialog's close event

Post by doublemax » Mon Jan 09, 2012 12:46 pm

The fix is to also define the event sink, which I assume, is the first place where the event is raised.
Defining the event sink is not really the fix.

The important difference for catching the event is the object for which Connect() is called.
When just using Connect(), you try catching the event in the MyApp object, where it never arrives.
When using dialog->Connect(), you catch the event directly where it originates, in the wxDialog.

The event sink makes sure that inside the event handling method, "this" points to the correct instance.

If you'd just use this:

Code: Select all

dialog->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MyApp::winClosing) );
, catching the event would work, but inside the MyApp::winClosing(), "this" would not point to an instance of MyApp, but to the dialog pointer. This would lead to crashes as soon as you'd try to use any member variables inside the method.
Use the source, Luke!

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: Can't connect to wxDialog's close event

Post by denarced » Mon Jan 09, 2012 1:40 pm

Hah! It's proven again: it's always worth the trouble to make sure you understood correctly because you usually didn't :D

Much thanks doublemax!

Post Reply