Passing data from Parent to Child

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
youssof.attallah
In need of some credit
In need of some credit
Posts: 1
Joined: Mon Apr 05, 2021 12:16 pm

Passing data from Parent to Child

Post by youssof.attallah »

So, I'm doing a todo-list as a uni project and I want to design a simple GUI for it. My question is, how can I force the the main frame to wait until the child frame is done before adding the data. I have a main frame which has the todo-list itself and buttons for the functionalities. When the user wants to add a task, he presses a button and it opens a new child frame where he can input the data. Supposedly the user will input the task, and then the childframe gets destroyed, and the todo-list (Implemented as a linked list) gets updated, however, whenever the add task button is pressed, it does not wait for the user to enter the input but rather adds the initial value for each aspect in the todo-list.
Code:
MainFrame:

Code: Select all

void cMain::OnAddButtonClicked(wxCommandEvent& evt) {
	add_task_frame = new cAdd(task,category,priority,day,month,year,hour,minute);//Takes the variables from the mainframe, passes them by ref to 
// child frame
	add_task_frame->Show();
	todo_list.add_node(priority, tskID, task, category, day, month - 1, year - 1900, hour, minute);
ChildFrame:

Code: Select all

void cAdd::AddTaskButtonClicked(wxCommandEvent& evt) {
	task = add_txt_task->GetValue();
	category = add_txt_ctg->GetValue();
	priority = wxAtoi(add_txt_prt->GetValue());
	day = wxAtoi(add_txt_day->GetValue());
	month = wxAtoi(add_txt_month->GetValue());
	year = wxAtoi(add_txt_year->GetValue());
	hour = wxAtoi(add_txt_hour->GetValue());
	minute = wxAtoi(add_txt_minute->GetValue());
	this->Close(true);
	evt.Skip();	
}
Last edited by catalin on Mon Apr 05, 2021 12:30 pm, edited 1 time in total.
Reason: code tags
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Passing data from Parent to Child

Post by doublemax »

The "child frame" you're creating should be a modal dialog.

Here's some sample code for a dialog for entering one string:

Code: Select all

class DataEntryDialog : public wxDialog
{
public:
  DataEntryDialog(wxWindow *parent, const wxString &title) : wxDialog(parent, wxID_ANY, title)
  {
    wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);

    m_textctrl = new wxTextCtrl(this, wxID_ANY, "");
    m_textctrl->SetHint( "Enter text here");

    mainSizer->Add(m_textctrl, 0, wxEXPAND|wxALL, 4);

    wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
      buttonSizer->AddStretchSpacer(1);   // make buttons right-aligned by adding flexible space to the left

      wxButton *cancelButton = new wxButton(this, wxID_CANCEL, "Cancel");
      buttonSizer->Add( cancelButton, 0, wxEXPAND|wxALL, 4 );

      wxButton *okButton = new wxButton(this, wxID_OK, "OK");
      okButton->SetDefault();
      buttonSizer->Add( okButton, 0, wxEXPAND|wxALL, 4 );
    mainSizer->Add(buttonSizer, 0, wxEXPAND|wxALL, 4);

    SetSizerAndFit(mainSizer);

    Bind(wxEVT_BUTTON, &DataEntryDialog::OnCommand, this);
    Bind(wxEVT_CLOSE_WINDOW, &DataEntryDialog::OnClose, this);
  };

  wxString GetValue() const { return m_textctrl->GetValue(); }

protected:
  void OnCommand( wxCommandEvent &event)
  {
    /*
      the switch is not really necessary in this sample,
      we could just call EndModal( event.GetId() ) here
      
      added just for clarity and demonstration purposes
    */
    switch(event.GetId())
    {
      case wxID_OK:
        EndModal(wxID_OK);
      break;

      case wxID_CANCEL:
        EndModal(wxID_CANCEL);
      break;

      default:
        EndModal(wxID_CANCEL);
      break;
    }
  };
  
  void OnClose( wxCloseEvent &event)
  {
    EndModal(wxID_CANCEL);
  };

  wxTextCtrl *m_textctrl;
};
And here is how you would use it:

Code: Select all

DataEntryDialog dlg(this, "enter some text");
int ret = dlg.ShowModal();

if( ret == wxID_OK )
{
  wxLogMessage("text entered: %s", dlg.GetValue() );
} else {
  wxLogMessage("dialog cancelled");
}
Use the source, Luke!
Post Reply