gpt4 book ai didi

c++ - 使用带有 wxEvtHandler::Bind 的模板自定义 wxEvent

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:52 25 4
gpt4 key购买 nike

在下面的代码中,我执行以下操作:

  • 我创建了一个新的模板 wxCommandEvent 类,它包含模板指定的一些有效载荷。
  • 然后我继承了一个 wxStringPayloadEvent 类。
  • 最后,我创建了一个示例应用程序,它只发出一个 wxStringPayloadEvent,然后事件处理程序被触发并在屏幕上的消息框中显示负载。

如果我使用稍微过时的 wxEvtHandler::Connect 方法(正如我在下面评论的那样),那么一切正常。但是如果我使用 wxEvtHandler::Bind 方法,我会收到一系列非常神秘的错误消息(在代码之后发布)。

因为 Bind 允许更多的自由并且更容易使用(它不需要创建笨拙的宏),所以我想使用它而不是 Connect ...有什么想法吗?

代码如下:

#include <wx/wx.h>

//Creating my own custom event which will hold a payload of some templated type
template <class Payload_type>
class TemplatedPayloadEvent : public wxCommandEvent
{
public:
TemplatedPayloadEvent(){}
TemplatedPayloadEvent(wxEventType eventType) : wxCommandEvent(eventType){}

TemplatedPayloadEvent(const TemplatedPayloadEvent& event)
: wxCommandEvent(event)
{
this->mPayload = event.mPayload;
}

virtual TemplatedPayloadEvent* Clone() const {return new TemplatedPayloadEvent(*this);}

void setPayload(Payload_type payload) {mPayload = payload;}
Payload_type getPayload() {return mPayload;}

private:
Payload_type mPayload;
};


//instantiating new event along with associated elements
class wxStringPayloadEvent : public TemplatedPayloadEvent<wxString>
{
public:
wxStringPayloadEvent() : TemplatedPayloadEvent<wxString>(wxEVT_STRING_PAYLOAD){};
};
typedef void (wxEvtHandler::*wxStringPayloadEventFunction)(wxStringPayloadEvent&);
#define wxStringPayloadEventHandler(func) \
(wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)\
wxStaticCastEvent(wxStringPayloadEventFunction, &func)
const wxEventType wxEVT_STRING_PAYLOAD = wxNewEventType();


//implementing test application
class MyApp : public wxApp
{
public:
virtual bool OnInit();
void OnProcessCustom(wxStringPayloadEvent& event);
};

bool
MyApp::OnInit()
{
//Connect event
//Connect(wxEVT_STRING_PAYLOAD, wxStringPayloadEventHandler(MyApp::OnProcessCustom));
Bind(wxEVT_STRING_PAYLOAD, &MyApp::OnProcessCustom, this);///< wish I could use this

wxStringPayloadEvent eventCustom;
eventCustom.SetEventObject(this);
eventCustom.setPayload(wxT("Test payload."));
wxPostEvent(this, eventCustom);

return true;
}

void MyApp::OnProcessCustom(wxStringPayloadEvent& event)
{
wxMessageBox(wxT("Event received. Payload = \"") + event.getPayload() + wxT("\""));
}

IMPLEMENT_APP(MyApp);

这是错误信息:

/ThirdParty/Includes/wx/event.h: In constructor 'wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>::wxEventFunctorMethod(void (Class::*)(EventArg&), EventHandler*) [with EventTag = int, Class = MyApp, EventArg = wxStringPayloadEvent, EventHandler = MyApp]':
/ThirdParty/Includes/wx/event.h:587: instantiated from 'wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>* wxNewEventFunctor(const EventTag&, void (Class::*)(EventArg&), EventHandler*) [with EventTag = int, Class = MyApp, EventArg = wxStringPayloadEvent, EventHandler = MyApp]'
/ThirdParty/Includes/wx/event.h:3182: instantiated from 'void wxEvtHandler::Bind(const EventTag&, void (Class::*)(EventArg&), EventHandler*, int, int, wxObject*) [with EventTag = wxEventType, Class = MyApp, EventArg = wxStringPayloadEvent, EventHandler = MyApp]'
../src/program.cpp:29: instantiated from here
/ThirdParty/Includes/wx/event.h:382: error: invalid conversion from 'wxEvent*' to 'wxStringPayloadEvent*'
/ThirdParty/Includes/wx/event.h:382: error: initializing argument 1 of 'static void wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>::CheckHandlerArgument(EventArg*) [with EventTag = int, Class = MyApp, EventArg = wxStringPayloadEvent, EventHandler = MyApp]'

更新更多线索:

  • This might be a clue,但我的 C++ 仍然不够好,无法确定。
  • 查看 event.h:382 我发现以下注释:

if you get an error here it means that the signature of the handler you're trying to use is not compatible with (i.e. is not the same as or a base class of) the real event class used for this event type

最佳答案

这可能是应该记录的内容,但据我所知,当您使用 Bind() 时,您不能仅使用“const wxEventType ...”来声明您的事件类型。 Bind() 需要使用 wxDEFINE_EVENT() 时提供的事件类型类。因此,您实际上需要在此处进行一些更改。

1) 使用它来定义您的事件类型(替换“const wxEventType ...”):

wxDEFINE_EVENT(wxEVT_STRING_PAYLOAD, wxStringPayloadEvent);

2) 显然需要在 wxStringPayloadEvent 定义之后完成上述操作,因此您的 wxStringPayloadEvent 构造函数不能仅默认为您的自定义事件类型。所以这里是 wxStringPayloadEvent 的构造函数应该看起来更像:

wxStringPayloadEvent() : TemplatedPayloadEvent<wxString>(){};
wxStringPayloadEvent(wxEventType eventType) : TemplatedPayloadEvent<wxString>(eventType){};

3) 因为你不能默认你的事件类型,你需要在你的自定义事件对象构造中指定它:

wxStringPayloadEvent eventCustom(wxEVT_STRING_PAYLOAD);

我已经用 SVN trunk 测试了这些变化,它似乎工作得很好。

关于c++ - 使用带有 wxEvtHandler::Bind 的模板自定义 wxEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com