Search code examples
c++wxwidgets

Communicating between 2 panels


While reading tutorial for wxWiidgets I saw this Tutorial where author has created 2 subclasses of wxPanel, LeftPanel and RightPanel.

Left has 2 buttons, clicking which the text on right updates. Now there is another class Communicate which has one member wxPanel of which left and right panel are child.

Left Panel has a method ->

void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event))
{
  count++;

  Communicate *comm = (Communicate *) m_parent->GetParent();
  comm->m_rp->m_text->SetLabel(wxString::Format(wxT("%d"), count));
}

where m_text is a public member of Right Panel.

I doubt if this is a good way of communicating. My Question - Is this correct way of communicating? What will be a better method?


Solution

  • I also saw the tutorial you are talking about. I think this is a good way of communicating as it uses the connect event methods of wxwidgets (read the events for more details). I'm no expert in wxwidgets, but i've always wrote my apps using this way of associating events of a class widgets to a function of the class and then this functions locate the object that it has to change the properties.

    Anyway if you consider that this event (of pushing the plus button) would happen really often and, specially, if you had too many functions/buttons like the plus and minus of the tutorial, one could define the m_text at the constructor:

    in header:

    wxStaticText *m_textlp;
    

    in construtor:

    m_textlp = ( (Communicate *) m_parent->GetParent() )->m_rp->m_text;
    

    such that the function would be only:

    void LeftPanel::OnPlus(wxCommandEvent & WXUNUSED(event))
    {
      count++;
      m_textlp->SetLabel(wxString::Format(wxT("%d"), count));
    }