Search code examples
c++wxwidgets

wxwidgets connect to function in member class


Header: I have got class with a testfunction and and a panel

class testsubclass{
      public:
         testsubclass();
         void testfunc(wxCommandEvent &event);
};

class panelonwindow : public wxPanel{
      public:
         panelonwindow(wxWindow *windowid, int ID);
         wxWindow *mywindow, *mypanel;
         wxTextCtrl *mytextcontrol;

         void maketextctrl(std::string label, int &id);
}

I would like the class to create a Textcontrol on my main window. As functions I use

testsubclass::testsubclass(){
}

panelonwindow::panelonwindow(wxWindow *windowid, int ID)
    :wxPanel(windowid, ID, wxDefaultPosition, wxSize(150, 150)){
        mywindow = windowid;
        mypanel = this;
};

void panelonwindow::maketextctrl(std::string label, int &id){
    wxString newlabel(label.c_str(), wxConvUTF8);
    mytextcontrol = new wxTextCtrl(mypanel, id, newlabel, wxDefaultPosition, wxSize(130, 30));
}


void testsubclass::testfunc(wxCommandEvent &event){
    printf("%s\n", "testfunc was called");
}

My main window header file contains pointers to both these classes:

Header:

wxWindow *mainwindow;
testsubclass *mysubclass;
panelonwindow *testpanel;
int ID1 = 100;
int ID2 = 101;

Now the main function looks like this:

mainwindow = this;
std::string textcontrolstring = "this is a test";
testpanel = new panelonwindow(mainwindow, ID);
testpanel->maketextctrl(textcontrolstring, ID2);

mysubclass = new testsubclass();

The problem is, I cannot link the testsublass function testfunc to an event like this from the main window function, I get some cryptic compiler messages, when I try to do something like this:

Connect(ID2, wxEVT_COMMAND_TEXT_UPDATED,
    wxCommandEventHandler(mysubclass->testfunc));

I can link to an event from within the void panelonwindow::maketextctrl to another member of panelonwindow functions (provided I declare them in a fashion like void panelonwindow::testfunc(wxCommandEvent &event)

void panelonwindow::maketextctrl(std::string label, int &id){
    wxString newlabel(label.c_str(), wxConvUTF8);
    mytextcontrol = new wxTextCtrl(mypanel, id, newlabel, wxDefaultPosition, wxSize(130, 30));

Connect(id, wxEVT_COMMAND_TEXT_UPDATED,
    CommandEventHandler(panelonwindow::testfunc))
}

Since I plan on creating a lot of buttons on a panel, I would prefer defining functions in member classes, rather than write a function for each button/textcontrolwindow separatey controlling all the windows.

It might be a bit of a newbie question, but I would appreciate any kind of help


Solution

  • You need to call Connect() on the object generating the event and pass it the object handling it (how else is wxWidgets going to figure out which object to send the event to? It can't read your code to find out). So to handle an evnet in testsubclass::testfunc() you need to call

    Connect(id, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(testsubclass::testfunc), NULL, mysubclass);
    

    but you still need to decide on which object do you want/need to call Connect().

    If you use wxWidgets 3 (and you should), consider using shorter event type names and more modern Bind() as it's more clear and shorter:

    Bind(wxEVT_TEXT, &testsubclass::testfunc, mysubclass, id);