Search code examples
c++wxwidgets

wxWidgets without wx main program -- how to code?


I'm trying to write a wxWidgets component of an app that is called by another part of the app, so that there is no wxWidgets main program. I am having some problems finding the correct sequence of calls.

As I understand it, first I need to subclass wxApp:

class TestApp : public wxApp {
public:
    virtual bool OnInit() override;
};
bool TestApp::OnInit()  {
    cerr << "app init\n"<<flush;
}

Then I need to do something like this when I want to start up wxWidgets:

    TestApp* app = new TestApp();
    cerr << "a\n";
    wxApp::SetInstance(app);
    cerr << "b\n";
    int argCount = 0;
    char* argv[0];
    if(! wxEntryStart(argCount, argv)) {
        cerr << "wx failed to start\n";
        wxExit();
    }
    cerr << "d\n";
    int res = wxGetApp().OnRun();

But it never calls OnInit(). Does anyone know what I should be doing?

This question is different from wxWidgets: How to initialize wxApp without using macros and without entering the main application loop? in that they do not want to call the event loop (so they want wxEntryStart()) but I do want the event loop (so, it turns out, I want wxEntry()).


Solution

  • wxEntryStart() indeed does not call OnInit(), only wxEntry() does.

    So you can either use wxEntry() (which also calls OnRun()) or call wxTheApp->CallOnInit() manually.

    See here for details: wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?