Search code examples
c++wxwidgets

Creating a pointer to a widget(wxTextCtrl) in a function


I've trying to get a pointer to a widget (in the code it's named text). But at least I've got only this

error: no matching function for call to 'std::basic_ostream::basic_ostream(wxWindow*)'

my code: gui.h

#include <wx/wx.h>

class wxWCK : public wxFrame
{
public:
    wxWCK(const wxString& title);

    void OnClickCon(wxCommandEvent& event);
    void OnClickSta(wxCommandEvent& event);

private:
    wxButton *connect;
    wxButton *start;
    wxTextCtrl *text;
};

const int ID_CON = 100;
const int ID_STA = 101;
const int ID_MF0 = 102;
const int ID_TEX = 103;

void Connect();
void Start();

and gui.cpp #include "gui.h"

wxWCK::wxWCK(const wxString& title)
    : wxFrame(NULL, ID_MF0, title, wxDefaultPosition, wxSize(400,300))
{
    wxPanel *panel = new wxPanel(this, -1);

    wxBoxSizer *vbox  = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);   

    //Outputwidget
    text = new wxTextCtrl(panel, ID_TEX, _T(""), 
        wxPoint(-1, -1), wxSize(1000, 1000), wxTE_MULTILINE);

    //redirecting stream to the outputwidget
    //std::ostream stream(text);    
    stream << "Hello" << std::endl;

    connect = new wxButton(panel, ID_CON, _T("Connect"));
    start   = new wxButton(panel, ID_STA, _T("Start"));

    hbox1->Add(text);
    hbox2->Add(connect);
    hbox2->Add(start);

    vbox->Add(hbox1, 1, wxEXPAND);
    vbox->Add(hbox2, 0, wxALIGN_LEFT | wxRIGHT | wxBOTTOM, 10);

    panel->SetSizer(vbox);

    Connect(ID_CON, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(wxWCK::OnClickCon));
    //Connect(ID_STA, wxEVT_COMMAND_BUTTON_CLICKED,
    //  wxCommandEventHandler(wxWCK::OnClickSta));
}

void wxWCK::OnClickCon(wxCommandEvent& WXUNUSED(event))
{   void Connect(); }

void wxWCK::OnClickSta(wxCommandEvent& WXUNUSED(event))
{   void Start();   }

void Connect()
{
    //Try to get a pointer to 'text'
    std::ostream stream(wxWindow::FindWindowById(ID_TEX));  
    stream << "Connected" << std::endl;
}
/*
void Start()
{
    //Try to get a pointer to 'text'
    std::ostream stream(wxWindow::FindWindowById(ID_TEX));  
    stream << "Started" << std::endl;
}
*/

I hope somebody can help me. A other solution can be to get the stream as global. But when I try to get the stream-declaration in the header, he says he dont know any text so I move the text-declaration outside the class and I got a muliple declaration error... I think, because I include the gui.h in gui.cpp and main.h . -Casisto

edit: I changed the the function to:

void Connect()
{
    //Try to get a pointer to 'text'
    std::ostream stream((wxTextCtrl*) wxWindow::FindWindowById(ID_TEX));
    stream << "Connected" << std::endl;
}

Now I don't get a error or a warning, but when I click on "Connect"-Button, the wxTextCtrl get no "Input" (I mean, there is only "Hello" in there)


Solution

  • The initial error must have been from the old code of Connect() because wxWindow::FindWindowById() returns a wxWindow*.

    First thing to try: change wxWCK::OnClickCon() to { (*text) << "Connected\n"; }. This should just work.

    Next, if you really need that void Connect(), try again without creating the std::ostream (you don't really need it); something like { (*(wxTextCtrl*)wxWindow::FindWindowById(ID_TEX)) << "Connected\n"; }. However, this might still not work, so make ID_CON = wxID_HIGHEST + 1, ID_STA = wxID_HIGHEST + 2 etc and try again. IIRC I've seen cases where ids with low values caused curious behaviour.