【问题标题】:Event handler function to wxButton with Bind() - wxWidgets 3.0带有 Bind() 的 wxButton 的事件处理函数 - wxWidgets 3.0
【发布时间】: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


    【解决方案1】:

    只要您希望您的应用开始处理预期的事件,您就可以Bind() 事件处理程序。 很多时候这是在构建 UI 之后完成的,所以在你的情况下,MainFrame() 构造函数的末尾应该没问题。

    您可以在示例中找到使用Bind() 的示例,就像许多其他 wxWidgets 功能一样;请参阅events 示例。 对于你的情况,你可以做

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

    【讨论】:

    • 感谢您的回答并为我指出正确的示例,我很感激。我将 Bind() 放在 MainFrame 构造函数的末尾,我得到一个错误“”无法在初始化中将 'MainFrame*' 转换为 'MainFrameFunctions*'| “”。你知道如何解决它吗?我使用 CodeBlocks 13.12。谢谢。
    • 您需要有一个指针,指向您想要将其方法绑定为处理程序的类,即MainFrameFunctions 而不是this,它可能是MainFrame 类型。当然,这假设您有意想要将处理程序分离到单独的类中(为什么?)。
    • 是的,确实,我只是没有注意到那里使用了两个不同的类。我已经更新了建议。
    • 我相信它会让代码看起来更方便,更容易阅读。但在您发表评论后,我发现可能并非如此。虽然我是初学者,但我缺乏良好的编程实践,所以在安排片段以使其可读时,我经常会迷失方向。谢谢两位,我现在明白了。
    • @catalin - 我确实更正了我的代码,所以它就像 VZ。建议。这意味着处理函数现在位于 Main Frame 类中。绑定函数现在是 ""buttonOne->Bind(wxEVT_BUTTON, &MainFrame::buttonOneClicked, this);"" 。但我收到此错误 ""no matching function for call to 'wxButton::Bind(const wxEventTypeTag&, bool (MainFrame::*)(wxCommandEvent&), MainFrame* const)'|"" 。你能从中得到什么吗?谢谢。
    猜你喜欢
    • 2014-07-14
    • 1970-01-01
    • 2019-03-26
    • 2013-01-13
    • 2015-11-18
    • 2014-08-30
    • 2019-08-27
    • 1970-01-01
    • 2011-01-31
    相关资源
    最近更新 更多