簡體   English   中英

具有Bind()的wxButton的事件處理函數-wxWidgets 3.0

[英]Event handler function to wxButton with Bind() - wxWidgets 3.0

我正在嘗試創建一個簡單的基本應用程序,以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)'|

您可以在希望應用程序開始處理預期事件時立即Bind()事件處理程序。 很多時候,這是在構建UI之后完成的,因此在您的情況下, MainFrame()構造函數的末尾應該沒問題。

您可以在示例中找到使用Bind()的示例,就像許多其他wxWidgets功能一樣。 查看events樣本。 對於你的情況,你可以做

buttonOne->Bind(wxEVT_BUTTON, &MainFrameFunctions::buttonOneClicked, aMainFrameFunctionsPointer);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM