質問

I am trying to make an input box for my GUI app using wxTextCtrl. I've made the box but when I'm trying to take the input text from it, it crashes and I can't figure out why.

Here is the code:

main.cpp:

int main(int argc, char** argv)
{   
   wxApp::SetInstance( new App() );
   wxEntryStart(argc, argv);
   wxTheApp->OnInit();

   wxTheApp->OnRun();

   wxTheApp->OnExit();
   wxEntryCleanup();

   return 0;
}

app.cpp:

bool App::OnInit()
{
   wxString title = "Version: ";
   title += APP_VERSION;

   /* Create the frame for my app */
   Frame *simple = new Frame(0, wxID_ANY, title, wxDefaultPosition, wxSize(287, 700)/*wxSize(287, 450)*/, (wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX)));
   simple->Show(true);

   return true;
}

frame.cpp:

Frame::Frame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) 
              : wxFrame( parent, id, title, pos, size, style )
{
   wxPanel  *panel  = new wxPanel(this, wxID_ANY);
   input_text  = new wxTextCtrl(this, wxID_ANY, "initial", wxPoint(10, 400), wxSize(250, 100));

   wxString str = input_text->GetValue();
   wxMessageBox(str);
}

It crashes in frame.cpp on the line: wxString str = input_text->GetValue();

I'm compiling on windows using wxWidgets-3.0.0 and Visual Studio 2012.

This may be something very simple but I just started working with wxWidgets and I don't have much experience with it.

Thanks.

役に立ちましたか?

解決

If you are just starting with wxWidgets, why don't you use the standard way of initializing it, i.e. wxIMPLEMENT_APP macro? I am not sure if this explains your problem (it would be really useful if you ran your program under debugger and determined why exactly does it crash, i.e. showed us the full stack trace), but you definitely don't initialize the library correctly.

FWIW you also create the text control as child of the frame when you probably meant to create it as child of the panel, but this is definitely not related to the crash neither.

他のヒント

You are calling GetValue() from within the Frames constructor, before the window was shown at all. I don't think it's possible at this time.

For anybody else arriving here:

I believe this might be caused by having the wxWidgets library (dlls/shared objects) compiled with different string settings than the application (wide/UTF-16/16bit vs. narrow/UTF-8/char).

I'm sorry I didn't have the time to investigate this further in my case, and just ended up compiling the libarary and app in the same tree But I believe this is the direction to look since it explains otherwise impossible to explain crashes related to wxString.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top