概括:
在将wxwidgets Hello World 教程中的代码改编为 CppMicroServices 框架中的“模块”后,使用 Event Table 或 Bind() 注册的事件似乎不会触发,但使用 Connect() 注册的事件会触发。
即当我单击菜单项时,我没有弹出。如果我Connect(...)
用来注册事件处理程序,弹出窗口确实有效。
为什么会发生这种情况,我该如何解决?
环境:
- Ubuntu 14.04
- g++ 4.9
- wxWidgets 3.0.2
- 二维码 3.0
代码:
Github 存储库- 用于完整源代码。它不容易构建(如果有助于诊断,请询问我的编译步骤)。
// 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 来管理我的依赖项,因此导入可能不是您习惯看到的。
我相信所有这些都在主线程中运行。
更新:
将openWindow(...)
调用从 BlankDisplayService 的构造函数转移到测试类会导致使用事件表声明的事件正确触发。但是我无法确定原因 - 无论是在测试中还是在构造函数中调用 threadId,它都是完全相同的:
当它不起作用时:
[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.