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.
This commit is contained in:
ali kettab
2022-11-26 17:00:45 +01:00
committed by Vadim Zeitlin
parent d458af3ca7
commit 7b245ccefc
+15
View File
@@ -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;
}