更新
This was a bug in wxWidgets。如果您使用的是兼容C ++ 11的编译器,则已在wxWidgets 3.1.1中修复此问题。
我正在尝试将事件处理程序动态绑定到wxWidgets中的事件。不幸的是,如果派生类受到保护,它似乎不起作用。
最小例子:
// Test.h
class Test : protected wxFrame
{
public:
Test();
private:
void sizing(wxSizeEvent& event);
};
// Test.cpp
Test::Test()
{
Bind(wxEVT_SIZING, &Test::sizing, this);
}
void Test::sizing(wxSizeEvent& event)
{
}
遗憾的是,这似乎不起作用,并在Visual Studio 2015 Update 3上发出以下错误:
wxWidgets\include\wx/meta/convertible.h(31): error C2243: 'type cast': conversion from 'Test *' to 'wxEvtHandler *' exists, but is inaccessible
wxWidgets\include\wx/event.h(335): note: see reference to class template instantiation 'wxConvertibleTo<Class,wxEvtHandler>' being compiled
with
[
Class=Test
]
wxWidgets\include\wx/event.h(3568): note: see reference to class template instantiation 'wxEventFunctorMethod<EventTag,Test,EventArg,EventHandler>' being compiled
with
[
EventTag=wxEventTypeTag<wxSizeEvent>,
EventArg=wxSizeEvent,
EventHandler=Test
]
Test.cpp(78): note: see reference to function template instantiation 'void wxEvtHandler::Bind<wxEventTypeTag<wxSizeEvent>,Test,wxSizeEvent,Test>(const EventTag &,void (__cdecl Test::* )(EventArg &),EventHandler *,int,int,wxObject *)' being compiled
with
[
EventTag=wxEventTypeTag<wxSizeEvent>,
EventArg=wxSizeEvent,
EventHandler=Test
]
将继承更改为public使其有效:
class Test : public wxFrame
答案 0 :(得分:1)
您可以使用以下方法解决此问题:
Bind(wxEVT_SIZING, std::bind(&Test::sizing, this, std::placeholders::_1));
编译的最小样本:
#include <wx/wx.h>
#include <functional>
using namespace std::placeholders;
class Test : protected wxFrame
{
public:
Test();
private:
void sizing(wxSizeEvent& event);
};
Test::Test()
{
Bind(wxEVT_SIZING, std::bind(&Test::sizing, this, _1));
}
void Test::sizing(wxSizeEvent& event)
{
}
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( "", wxPoint(50, 50), wxSize(450, 340) );
frame->Show( true );
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size)
{
Test* test = new Test();
}
答案 1 :(得分:1)
这确实看起来像是wxWidgets事件处理代码中的一个缺陷,因为错误来自wxConvertibleTo
完成的检查,以确定Test
是否来自wxEvtHandler
(此处它不是#39; t公开做。)
我可以推荐的最简单的修复方法是使用一个临时的lambda,绕过这个检查,例如:这工作(当然,假设您使用C ++ 11):
#include <wx/frame.h>
class Test : protected wxFrame {
public:
Test() { Bind(wxEVT_SIZING, [=](wxSizeEvent& e) { sizing(e); }); }
private:
void sizing(wxSizeEvent&) { }
};