I am making a crossplatform c++ wxwidgets desktop app, and I got a problem on Windows,…
- User
I am making a crossplatform c++ wxwidgets desktop app, and I got a problem on Windows, Linux works perfectly. I have a method that detects if a wxCombobox was edited and then do something. The problem on Windows is that it calls this method as soon as the frame is loaded and the wxCombobox was initialized with the initial value, where on Linux it doesn't call it.
The method is this: void Frame::OnCombobox(wxCommandEvent& event) { if ((event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED || event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED) && event.GetSelection() != wxNOT_FOUND) { SetEdited(); }
event.Skip();}
And the callstack is this: #0 ?? Frame::OnCombobox (this=0x2ba5ff0, event=...) #1 0x698c3946 wxAppConsoleBase::HandleEvent(this=0x2b63490, handler=0x2ba5ff0, func=&virtual table offset 2232, event=...) (../../src/common/appbase.cpp:657) #2 0x698c39c7 wxAppConsoleBase::CallEventHandler(this=0x2b63490, handler=0x2ba5ff0, functor=..., event=...) (../../src/common/appbase.cpp:669) #3 0x6999a7d2 wxEvtHandler::ProcessEventIfMatchesId(entry=..., handler=0x2ba5ff0, event=...) (../../src/common/event.cpp:1425) #4 0x6999b4e8 wxEvtHandler::SearchDynamicEventTable(this=0x2ba79b0, event=...) (../../src/common/event.cpp:1897) #5 0x6999ac45 wxEvtHandler::TryHereOnly(this=0x2ba79b0, event=...) (../../src/common/event.cpp:1618) #6 0x699ddc6c wxEvtHandler::TryBeforeAndHere(this=0x2ba79b0, event=...) (../../include/wx/event.h:3927) #7 0x6999aaca wxEvtHandler::ProcessEventLocally(this=0x2ba79b0, event=...) (../../src/common/event.cpp:1555) #8 0x6999aa66 wxEvtHandler::ProcessEvent(this=0x2ba79b0, event=...) (../../src/common/event.cpp:1528) #9 0x6999ad36 wxEvtHandler::SafelyProcessEvent(this=0x2ba79b0, event=...) (../../src/common/event.cpp:1646) #10 0x6cf56f0a wxWindowBase::HandleWindowEvent(this=0x2ba79b0, event=...) (../../src/common/wincmn.cpp:1570)
- Assistant
It seems like the issue you're facing is that the
OnComboboxmethod is being called on Windows as soon as the frame is loaded, even though the combobox wasn't actually edited. On Linux, this behavior doesn't occur.To fix this issue, you can modify your
OnComboboxmethod as follows:void Frame::OnCombobox(wxCommandEvent& event) { if (event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED || event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED) { if (GetCombobox()->FindString(GetCombobox()->GetValue()) != wxNOT_FOUND) { SetEdited(); } } event.Skip(); }In this modified version, we check if the selected text in the combobox is present in the list of available items. If it is, then we consider it as an actual edit and call the
SetEditedmethod.Additionally, make sure to replace
GetCombobox()with the actual method you're using to access the wxCombobox instance in your frame. - User
there is a problem with this way, the combobox is loaded and is assigned an initial element from the available items, so it has a value, what I wanted to check with this method is that it was done manually by the user then it is edited or done by the code which is not edited.
- Assistant
I see what you mean. In that case, you can keep track of whether the combobox has been manually edited by using a boolean flag. Here's an updated version of the
OnComboboxmethod:void Frame::OnCombobox(wxCommandEvent& event) { static bool editedByUser = false; // Flag to track manual editing if (event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED || event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED) { if (editedByUser) { SetEdited(); } else { // User has manually edited the combobox editedByUser = true; } } event.Skip(); }In this version, we initially set
editedByUsertofalse. When the combobox is selected or its text is updated, we check whethereditedByUseristrueorfalse. If it istrue, it means the combobox has been manually edited, and we call theSetEditedmethod. If it isfalse, it means the user has just manually edited the combobox, so we seteditedByUserto to indicate that subsequent changes should be considered as manual edits. - User
This method is really wrong, the bool will always be false since it is set as soon as it enter. Can you use some wxEVT_COMMAND_COMBOBOX_SELECTED or similar to filter which one was manually updated or which doesn't? Also why did you remove && event.GetSelection() != wxNOT_FOUND)? That was a way to detect if it was edited
- Assistant
Apologies for the confusion. To track manual edits of the combobox in a cross-platform manner, you can utilize the
FindStringfunction to check if the currently selected value exists in the combobox's list of options. Here's an updated version of theOnComboboxmethod:void Frame::OnCombobox(wxCommandEvent& event) { wxString selectedValue = event.GetString(); int selectedIndex = event.GetSelection(); // Only consider the event if a valid selection is made if (selectedIndex != wxNOT_FOUND) { wxArrayString comboBoxItems; GetCombobox()->GetStrings(comboBoxItems); // Check if the selected value exists in the list of combobox items if (comboBoxItems.Index(selectedValue) != wxNOT_FOUND) { SetEdited(); } } event.Skip(); }In this updated version, we first retrieve the selected value and index from the event. Then, we obtain all the strings that are part of the combobox's list of items using the function. By using , we can check if the selected value exists in the list of combobox items. If it does, we call the method to indicate that there has been a manual edit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.