I’m trying to create a GUI with wxWidgets in C++, where the user can double click on the main panel to add and remove circles. When a user single clicks on one of these circles, I want it to toggle isSelected
and change its appearance, as well as de-selecting all the other circles and changing their appearance.
To do this, I’ve made a custom widget inheriting wxWindow to control the appearance. I can get it to work where clicking on a CustomWidgetCircle
element will make it selected, and clicking on the background panel WidgetPanel
will deselect all, but I am not able to figure out how to get the WidgetPanel
to respond and change its children’s data by clicking on a CustomWidgetCircle
.
Code excerpts below. Right now, dynamic bindings are happening in the respective constructors.
// CustomWidgetCircle.h
class CustomWidgetCircle : public wxWindow {
public:
CustomWidgetCircle(wxPanel* parent, wxString label, wxPoint location);
bool isSelected = false;
private:
void OnMouseDown(wxMouseEvent& event);
void OnPaintEvent(wxPaintEvent& event);
void Render(wxDC& dc);
};
// CustomWidgetCircle.cpp
...
CustomWidgetCircle
CustomWidgetCircle::OnMouseDown(wxMouseEvent& event) {
this->isSelected = !this->isSelected;
Refresh();
}
...
// WidgetPanel.h
class WidgetPanel : public wxPanel {
public:
WidgetPanel(wxFrame* parent);
private:
void OnDoubleClick(wxMouseEvent& event);
void OnMouseDown
std::list<CustomWidgetCircle*> CustomWidgetCircleList;
};
// WidgetPanel.cpp
...
WidgetPanel::OnDoubleClick(wxMouseEvent& event) {
CustomWidgetCircle* newCircle = new CustomWidgetCircle(this, "lbl", event.GetPosition());
CustomWidgetCircleList.push_back(newCircle);
Refresh();
}
WidgetPanel::OnMouseDown(wxMouseEvent& event) {
for (auto circle_ptr : CustomWidgetCircleList) {
circle_ptr->isSelected = false;
}
Refresh();
}
...
What I’ve tried:
- Using
Skip()
in the call toOnMouseDown
on the child windows to pass the event up to the parent. Seems to do nothing. - Only binding the
OnMouseDown
handler to the parent frame and callingGetEventObject()
on the event arg in order to modify the child window that’s been clicked on, but that won’t work because it returns awxObject
so I can’t access member methods / data for my custom class. - Calling the
OnMouseDown
handler on the parent from the child window, but I can’t work out how to make parent methods accessible from the child. - Subclassing wxCommandEvent so that I can pass an event that propagates up the window hierarchy. I don’t think I’ve worked out how to properly define and initialize custom events yet.
Fully admit I’m missing a fair amount on how event handling works, but I’ve been through the docs several times now and am no closer. If any of the above could work and I’m missing some details, please let me know. I can provide more code to make it reproducible but it’s not a bug per se. Thanks in advance.
Bill DeBlazz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.