wxwidgets事件表/ Bind()事件似乎不会触发,Connect()会触发

时间:2015-04-19 07:16:06

标签: c++ wxwidgets biicode

要点:

wxwidgets Hello World tutorial中的代码调整为"模块"在CppMicroServices框架中,使用事件表或Bind()注册的事件似乎不会触发,但使用Connect()注册的事件会触发。

即。当我点击菜单项时,我不会弹出。如果我使用Connect(...)注册事件处理程序,则弹出窗口可以正常工作。

为什么会发生这种情况,我该如何解决?

环境:

  • Ubuntu 14.04
  • g ++ 4.9
  • wxWidgets 3.0.2
  • biicode 3.0

代码:

Github repository - 完整来源。它不容易构建(如果它有助于诊断,请询问我的编译步骤。)

// The following are excerpts, I haven't included the namespace declarations / #includes

// === BlankDisplayService.cpp === //
// I know the constructor is "heavy", but this is just a spike
// For this issue, this is the entry point
BlankDisplayService::BlankDisplayService() {
    wxApp::SetInstance( new BlankApplication() );
    openWindow(0, nullptr);
}

BlankDisplayService::~BlankDisplayService() {
    wxTheApp->OnExit();
    wxEntryCleanup();
}

int BlankDisplayService::openWindow(int argc, char** argv) {
    wxEntryStart(argc, argv);
    wxTheApp->OnInit();
    wxTheApp->OnRun();
    return 0;
}



// === BlankApplication.cpp === //
bool BlankApplication::OnInit() {
    BlankWindow *window = new BlankWindow( "Hello World", wxPoint(50, 50), wxSize(450, 340));
    window->Show(true);
    return true;
}


// === BlankWindow.h === //
class BlankWindow : public wxFrame {
private:
    wxStaticText *st1;
    wxStaticText *st2;

public:
    enum Command {
        ID_Hello = 1
    };

    BlankWindow(const wxString& title, const wxPoint& pos, const wxSize& size);
    virtual ~BlankWindow();

private:
    void OnMove(wxMoveEvent & event);
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
    wxDECLARE_EVENT_TABLE();
};


// === BlankWindow.cpp === //

wxBEGIN_EVENT_TABLE(sl::desktop::demo::wx::BlankWindow, wxFrame)
    EVT_MENU(ID_Hello,   BlankWindow::OnHello)
    EVT_MENU(wxID_EXIT,  BlankWindow::OnExit)
    EVT_MENU(wxID_ABOUT, BlankWindow::OnAbout)
wxEND_EVENT_TABLE()

BlankWindow::BlankWindow(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size) {
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );
    menuBar->Append( menuHelp, "&Help" );
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( "Welcome to wxWidgets!" );


    wxPanel* panel = new wxPanel(this, -1);
    panel->SetSize(200, 100);

    st1 = new wxStaticText(panel, -1, wxT(""), wxPoint(10, 10));
    st2 = new wxStaticText(panel, -1, wxT(""), wxPoint(10, 30));

    Connect(wxEVT_MOVE, wxMoveEventHandler(BlankWindow::OnMove));

    Centre();

    //Connect(wxEVT_MENU, wxCommandEventHandler(BlankWindow::OnHello));
}

BlankWindow::~BlankWindow() {
}

void BlankWindow::OnMove(wxMoveEvent& event) {
    wxPoint size = event.GetPosition();
    st1->SetLabel(wxString::Format(wxT("x: %d"), size.x ));
    st2->SetLabel(wxString::Format(wxT("y: %d"), size.y ));
}

void BlankWindow::OnHello(wxCommandEvent& WXUNUSED(event)) {
    wxLogMessage("Hello world from wxWidgets!");
}

void BlankWindow::OnExit(wxCommandEvent& WXUNUSED(event)) {
    Close(true);
}

void BlankWindow::OnAbout(wxCommandEvent& event) {
    wxMessageBox("This is a wxWidgets' Hello world sample", "About Hello World", wxOK | wxICON_INFORMATION );
}

备注:

我使用biicode管理我的依赖项,因此导入可能不是您以前常常看到的。

我确信所有这些都在主线程中运行。


更新

将BlankDisplayService的构造函数中的openWindow(...)调用转移到测试类中,导致使用事件表声明的事件正确触发。但是我还没能确定原因 - 无论是从测试中调用还是在构造函数中调用threadIds都完全相同:

什么时候不起作用:

[MAIN] Thread Id: 140460630185856
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SlDesktopDemoWxBundle
[ RUN      ] SlDesktopDemoWxBundle.DisplaysWindow
[TEST] Thread Id: 140460630185856
[CSTR] Thread Id: 140460630185856
[OPEN] Thread Id: 140460630185856
^C

什么时候有效:

[CSTR] Thread Id: 139695227554368
[MAIN] Thread Id: 139695227554368
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SlDesktopDemoWxBundle
[ RUN      ] SlDesktopDemoWxBundle.DisplaysWindow
[TEST] Thread Id: 139695227554368
[OPEN] Thread Id: 139695227554368
[       OK ] SlDesktopDemoWxBundle.DisplaysWindow (3165 ms)
[----------] 1 test from SlDesktopDemoWxBundle (3165 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3166 ms total)
[  PASSED  ] 1 test.

1 个答案:

答案 0 :(得分:0)

问题可能出在您的初始化代码中。很难看到你到底想要在这里实现什么,因为你最终调用OnRun(),无论如何进入正常的事件循环,为什么你不使用正常模式? / p>

我也不知道您的代码在何处/如何退出事件循环。如果它太快,它可以解释为什么你没有得到这些事件。