WXWidgets Event Arguments

拟墨画扇 提交于 2019-12-24 11:53:51

问题


I'm trying to set up a window in WXWidgets with a grid of buttons. Each of these buttons will open another window. Now I know I can do this by having the call to each window in a separate function depending on the button that was pressed. But that seems a bit inefficient.

What I want to do is have each of these buttons call the same function when clicked, but pass an ID which will be used to determine which window is opened up.

The basic code I'm using is as follow (bits are stripped out, I have a button that closes the application, just to test buttons out)

Simple::Simple(const wxString& title, int x, int y)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(x,y))
{

    wxPanel  *testPanel  = new wxPanel(this,wxID_ANY, wxDefaultPosition,wxSize(270, 150));
    wxButton *testButton = new wxButton(testPanel, wxID_EXIT, wxT("Actors"), wxPoint(20,20));
    Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(Simple::eventWindowCall));
    testButton->SetFocus();

    Centre();
}

void Simple::eventWindowCall(wxCommandEvent & WXUNUSED(event))
{
    Close(true); //just a line to make sure this function is being called
}

I've already tried using a number in place of an event ID when using connect, but the button's function doesn't get called if I do that. Ideally, I could just do something like wxCommandEventHandler(Simple::eventWindowCall(26)) and put a case statement in the eventWindowCall function that would show the correct window based on the number pased. But so far, that aproach has also been inefective.

Any advice you can offer would be great. Thanks for reading this. I've been working on figuring this out for hours.


回答1:


int wxEvent::GetId() const

Returns the identifier associated with this event, such as a button command id.

You can get the control ID related to the event from the wxCommandEvent that is passed to the function:

void eventWindowCall(wxCommandEvent& event) {
    event.GetId(); // <-
}

This will give you the ID of the button being pressed, in your case wxID_EXIT (since that is the ID you assigned to the button):

new wxButton(testPanel, wxID_EXIT
//                      ^^^^^^^^^ this will be passed as event id

Refer to the documentation of wxCommandEvent and its base class, wxEvent for more information.



来源:https://stackoverflow.com/questions/14075666/wxwidgets-event-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!