没有wx主程序的wxWidgets——如何编码?

wxWidgets without wx main program -- how to code?

我正在尝试编写一个由应用程序的另一部分调用的应用程序的 wxWidgets 组件,因此没有 wxWidgets 主程序。我在寻找正确的调用顺序时遇到了一些问题。

据我了解,首先我需要继承 wxApp:

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

那么当我想启动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();

但它从不调用 OnInit()。有谁知道我应该做什么?

这个问题与wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?的不同之处在于他们不想调用事件循环(所以他们想要wxEntryStart() ) 但我 do 想要事件循环(所以,事实证明,我想要 wxEntry())。

wxEntryStart() 确实不调用 OnInit(),只有 wxEntry() 调用。

因此您可以使用 wxEntry()(也调用 OnRun())或手动调用 wxTheApp->CallOnInit()

详情请看这里:wxWidgets: How to initialize wxApp without using macros and without entering the main application loop?