【发布时间】:2014-09-12 03:32:13
【问题描述】:
我正在尝试创建一个简单的基本应用程序,以从 wxWidgets 开始。我已经可以设置一个框架并在其上放置菜单、菜单项和按钮,并且它们都可以正常显示。
但是,我在使用 Bind() 将事件处理程序附加到按钮时遇到了麻烦。为了稍微剥离代码,我有这个:
class MainFrameFunctions
{
public:
void buttonOneClicked(wxCommandEvent & event);
};
void MainFrameFunctions::buttonOneClicked(wxCommandEvent & event)
{
// do something
}
MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& ) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Show Box"), wxPoint(70,50), wxSize(200,40) );
}
所以我想知道,如何使用 Bind() 创建事件处理程序,以将处理程序函数“buttonOneClicked”连接到 buttonOne?还有我应该把 Bind() 行放在代码的什么地方?
编辑
经VZ推荐。我已经对程序进行了一些编辑,因此主要组件现在看起来像这样:
class MainFrame: public wxFrame
{
public:
MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint
&pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE, const wxString &name=wxFrameNameStr);
void OnExit(wxCommandEvent& event);
bool ButtonOneClicked(wxCommandEvent & event);
wxDECLARE_EVENT_TABLE();
};
class CreateNewFrame: public wxFrame
{
public:
CreateNewFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize&, long style);
void OnExit(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
MainFrame::MainFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint& , const wxSize& , long style, const wxString& )
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size1)
{
wxButton * buttonOne = new wxButton( panelOne, buttonOneID, wxT("Button One"), wxPoint(70,50), wxSize(200,40), 0, wxDefaultValidator, "PP" );
// while we are at it, what is the " wxDefaultValidator, "PP" " part about ? I just copied it from an example
buttonOne->Bind(wxEVT_BUTTON, &MainFrame::ButtonOneClicked, this);
}
bool MainFrame::ButtonOneClicked(wxCommandEvent & event)
{
CreateNewFrame* NewFrame = new CreateNewFrame(this, NewFrameID, "Button One Clicked", position2, size2);
NewFrame->Show( true );
return true;
}
现在的问题是,我从 CodeBlocks 13.12 收到此错误消息:
no matching function for call to 'wxButton::Bind(const wxEventTypeTag<wxCommandEvent>&, bool (MainFrame::*)(wxCommandEvent&), MainFrame* const)'|
【问题讨论】:
标签: c++ events button event-handling wxwidgets