I'm learning wxWidgets and would like to know which event, connect or using static event tables? Why will you choose one from the other

Dani AI

Generated

Short answer: for new wxWidgets code prefer Bind(); static event-table macros remain supported but are mostly legacy, and Connect() is a historical, less-safe API. Bind() is the modern, flexible way to attach handlers (it supports lambdas, free functions and binding to methods on other objects), while event tables are concise when all handlers are static and belong to the same class. (wxwidgets.org)

Clarifying the tradeoffs: event tables centralize mappings and keep trivial handlers visible in one place, which is helpful for simple classes (as observed). Bind() is type-safer, checks signatures at compile time, permits runtime connect/disconnect, and fits modern C++ styles (including lambdas). Connect() offers no real advantages over Bind() and is not recommended for new code. (fossies.org)

Example patterns (minimal):

/* static event table (classic) */
class MyFrame : public wxFrame {
    void OnBtn(wxCommandEvent&);
    wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_BUTTON(wxID_ANY, MyFrame::OnBtn)
wxEND_EVENT_TABLE()
/* modern: Bind (recommended) */
MyFrame::MyFrame() {
    m_btn = new wxButton(this, wxID_ANY, "Go");
    m_btn->Bind(wxEVT_BUTTON, &MyFrame::OnBtn, this);
    /* or a lambda for tiny handlers */
    m_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent&){ /* quick action */ });
}

Practical tips: prefer a single style per codebase; use Bind() for dynamic UIs, glue to handlers in other objects, or C++11+ idioms; use event tables when a class truly benefits from a static, centralized map. Remember command events propagate up the parent chain (so handler placement matters), and handlers bound with Bind() can be Unbind()ed at runtime (static table mappings cannot). For details on propagation and Unbind semantics see the event-handling docs. (hokein.github.io)

Recommended Answers

All 3 Replies

I'm learning wxWidgets and would like to know which event, connect or using static event tables? Why will you choose one from the other

Which of the two types of event system you guys suggest? Is it using EVENT TABLE or CONNECT?

Thanks alot

I personally generally use an EVENT_TABLE if I have a large number of events for a class ( > 5 or so), but if I only have 1 or 2 I generally use Connect (or I have a complicated gui, lots of enabling/disabling etc). I've just found it to look cleaner: prevents a ridiculous amount of event tables but if there's a ton of events it's easier to keep them straight in an event table. That's my preference, there are some different things that can be done with connect so it's not a straight substitute either way, but if you want something more than that, here's explaining the subtle differences:

http://wxwidgets.blogspot.com/2007/01/in-praise-of-connect.html

Also, another interesting bit of info is that in my wxwidgets book, written by the creators, all of their examples (that I've seen) use event tables. It's possible that there's performance differences (significant I mean) since event tables are static routing and connect is dynamic but I have yet to see anything definitive so I'm going by what I like :)

hope that's helpful

~J

commented: Good post +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.