[英]Can somebody help me with EventHandling on WXWidgets
while trying to make some first progress with WXWidgets in C++ I´ve got on a problem.在尝试在C++中使用WXWidgets取得一些初步进展时,我遇到了一个问题。
As youre able to see in the Code below, I´m trying to Handle an Button-Click-Event from an WXWidget-Application.正如您在下面的代码中看到的那样,我正在尝试处理来自WXWidget-Application 的按钮单击事件。
The main Problem is that the dedicated Method TopPanel::OnClick()
doesnt run.主要问题是专用方法
TopPanel::OnClick()
没有运行。 (Nothing is print to console) (没有任何东西打印到控制台)
Possible Errors:可能的错误:
#include <wx/wx.h>
class App : public wxApp
{
public:
virtual bool OnInit();
};
class MainFrame : public wxFrame
{
public:
MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};
class TopPanel : public wxPanel
{
public:
TopPanel(wxWindow* parent, wxSize size);
private:
void OnClick(wxCommandEvent &);
wxDECLARE_EVENT_TABLE();
};
enum ButtonID {
button_id_first = wxID_LAST + 1,
button_id_other
};
bool App::OnInit()
{
MainFrame* frame = new MainFrame("Nova Loader", wxDefaultPosition, wxDefaultSize);
frame->Show(true);
return true;
}
wxIMPLEMENT_APP(App);
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(nullptr, wxID_ANY, title, pos, size)
{
this->SetBackgroundColour(wxColor(25, 25, 25));
this->SetFocus();
TopPanel* pnl_top = new TopPanel(this, wxSize(500, 30));
wxPanel* pnl_bottom = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(500, 270));
wxButton* btn_load = new wxButton(pnl_top, button_id_first, "Load");
wxButton* btn_exit = new wxButton(pnl_top, button_id_other, "Exit");
wxBoxSizer* s1 = new wxBoxSizer(wxVERTICAL);
s1->Add(pnl_top, 0, wxALL, 5);
s1->Add(pnl_bottom, 1, wxALL, 5);
wxBoxSizer* s2 = new wxBoxSizer(wxHORIZONTAL);
s2->Add(btn_load, 0, wxRIGHT, 5);
s2->Add(btn_exit, 0);
wxBoxSizer* s3 = new wxBoxSizer(wxVERTICAL);
s3->Add(s2, 0, wxALIGN_RIGHT | wxRIGHT | wxTOP, 3);
pnl_top->SetSizer(s3);
pnl_bottom->SetBackgroundColour(wxColor(45, 45, 45));
this->SetSizerAndFit(s1);
}
TopPanel::TopPanel(wxWindow* parent, wxSize size) : wxPanel(parent, wxID_ANY, wxDefaultPosition, size)
{
this->SetSize(500, 30);
this->SetId(wxID_ANY);
this->SetBackgroundColour(wxColor(45, 45, 45));
}
void TopPanel::OnClick(wxCommandEvent &e)
{
std::cout << e.GetId() << std::endl;
}
wxBEGIN_EVENT_TABLE(TopPanel, wxPanel)
EVT_BUTTON(button_id_first, TopPanel::OnClick)
EVT_BUTTON(button_id_other, TopPanel::OnClick)
wxEND_EVENT_TABLE()
Thanks to everyone trying to help me!感谢所有试图帮助我的人!
~ Tim 〜蒂姆
First things first, your given example successfully calls the OnClick
method.首先,您给定的示例成功调用
OnClick
方法。 So your claim that OnClick
is not called is not correct.因此,您关于未调用
OnClick
的说法是不正确的。
Now that being said, with newer(modern) wxWidgets
you can also use dynamic event table using Bind
instead of static event table as shown below.话虽如此,使用较新的(现代)
wxWidgets
,您还可以使用Bind
使用动态事件表而不是静态事件表,如下所示。 The changes made are highlighted as comments in the below given program:所做的更改在以下给定程序中突出显示为注释:
class TopPanel : public wxPanel
{
public:
TopPanel(wxWindow* parent, wxSize size);
void OnClick(wxCommandEvent &);
//removed wxdeclare_event_table from here as no longer needed
};
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(nullptr, wxID_ANY, title, pos, size)
{
//other code as before
//---------------------------------------------vvvvvvvv----------->let wxwidgets choose the id for you
wxButton* btn_load = new wxButton(pnl_top, wxID_ANY, "Load");
//bind btn_load to onClick
btn_load->Bind(wxEVT_BUTTON, &TopPanel::OnClick, pnl_top);
//---------------------------------------------vvvvvvvv------------>let wxwidgets choose the id for you
wxButton* btn_exit = new wxButton(pnl_top, wxID_ANY, "Exit");
//bind btn_exit to onclick
btn_exit->Bind(wxEVT_BUTTON, &TopPanel::OnClick, pnl_top);
//other code as before
}
Below is the complete working example that uses Bind
:下面是使用
Bind
的完整工作示例:
#include <wx/wx.h>
class App : public wxApp
{
public:
virtual bool OnInit();
};
class MainFrame : public wxFrame
{
public:
MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
};
class TopPanel : public wxPanel
{
public:
TopPanel(wxWindow* parent, wxSize size);
void OnClick(wxCommandEvent &);
//removed wxdeclare_event_table from here as no longer needed
};
enum ButtonID {
button_id_first = wxID_LAST + 1,
button_id_other
};
bool App::OnInit()
{
MainFrame* frame = new MainFrame("Nova Loader", wxDefaultPosition, wxDefaultSize);
frame->Show(true);
return true;
}
wxIMPLEMENT_APP(App);
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(nullptr, wxID_ANY, title, pos, size)
{
this->SetBackgroundColour(wxColor(25, 25, 25));
this->SetFocus();
TopPanel* pnl_top = new TopPanel(this, wxSize(500, 30));
wxPanel* pnl_bottom = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(500, 270));
//---------------------------------------------vvvvvvvv----------->let wxwidgets choose the id for you
wxButton* btn_load = new wxButton(pnl_top, wxID_ANY, "Load");
//bind btn_load to onClick
btn_load->Bind(wxEVT_BUTTON, &TopPanel::OnClick, pnl_top);
wxButton* btn_exit = new wxButton(pnl_top, wxID_ANY, "Exit");
//bind btn_exit to onclick
btn_exit->Bind(wxEVT_BUTTON, &TopPanel::OnClick, pnl_top);
wxBoxSizer* s1 = new wxBoxSizer(wxVERTICAL);
s1->Add(pnl_top, 0, wxALL, 5);
s1->Add(pnl_bottom, 1, wxALL, 5);
wxBoxSizer* s2 = new wxBoxSizer(wxHORIZONTAL);
s2->Add(btn_load, 0, wxRIGHT, 5);
s2->Add(btn_exit, 0);
wxBoxSizer* s3 = new wxBoxSizer(wxVERTICAL);
s3->Add(s2, 0, wxALIGN_RIGHT | wxRIGHT | wxTOP, 3);
pnl_top->SetSizer(s3);
pnl_bottom->SetBackgroundColour(wxColor(45, 45, 45));
this->SetSizerAndFit(s1);
}
TopPanel::TopPanel(wxWindow* parent, wxSize size) : wxPanel(parent, wxID_ANY, wxDefaultPosition, size)
{
this->SetSize(500, 30);
this->SetId(wxID_ANY);
this->SetBackgroundColour(wxColor(45, 45, 45));
}
void TopPanel::OnClick(wxCommandEvent &e)
{
std::cout << e.GetId() << std::endl;
std::cout<<"toppanel onclick called"<<std::endl;
}
//no need for event table entries here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.