From 7b245ccefc71db4d2ccfe89d5570524dbf68fa01 Mon Sep 17 00:00:00 2001 From: ali kettab Date: Thu, 24 Nov 2022 22:49:57 +0100 Subject: [PATCH] Ensure that a wxRadioButton in a group is always selected in wxQt This is not done by default by Qt, but wxWidgets expects to always have exactly one selected radio button in each radio button group. Fixing this allows the related unit test to pass for wxQt now. Closes #22991. --- src/qt/radiobut.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/qt/radiobut.cpp b/src/qt/radiobut.cpp index 53eced5ac7..de48011eda 100644 --- a/src/qt/radiobut.cpp +++ b/src/qt/radiobut.cpp @@ -32,16 +32,26 @@ void QtStartNewGroup(QRadioButton* qtRadioButton) // parent // QRadioButton is destroyed. QButtonGroup* qtButtonGroup = new QButtonGroup(qtRadioButton); qtButtonGroup->addButton(qtRadioButton); + + // From QButtonGroup documentation: + // If you create an exclusive button group, you should ensure that one of + // the buttons in the group is initially checked; otherwise, the group will + // initially be in a state where no buttons are checked. + + qtRadioButton->setChecked(true); } bool QtTryJoiningExistingGroup(wxRadioButton* radioBtnThis) { + bool checkRadioBtn = true; + for ( wxWindow* previous = radioBtnThis->GetPrevSibling(); previous; previous = previous->GetPrevSibling() ) { if ( wxRadioButton* radioBtn = wxDynamicCast(previous, wxRadioButton) ) { + checkRadioBtn = false; // We should never join the exclusive group of wxRB_SINGLE button. if ( !radioBtn->HasFlag(wxRB_SINGLE) ) { @@ -56,6 +66,11 @@ bool QtTryJoiningExistingGroup(wxRadioButton* radioBtnThis) } } + // Make sure radioBtnThis will be initially checked if there is no group + // to add it to. + if ( checkRadioBtn ) + radioBtnThis->SetValue(true); + return false; }