Fix wxRadioButton grouping in wxQt

Create QButtonGroup as needed to honour wxRB_GROUP and wxRB_SINGLE
styles.

Co-Authored-By: Cătălin Răceanu <maildus@gmail.com>

Co-Authored-By: liamtreacy <liam.treacy@gmail.com>
This commit is contained in:
Vadim Zeitlin 2019-02-02 15:04:16 +01:00
parent a913393147
commit 27f1c8f445

View File

@ -12,6 +12,53 @@
#include "wx/qt/private/converter.h"
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QButtonGroup>
namespace
{
// Tiny helper applying a safe cast to return the handle of wxRadioButton as
// its actual type.
inline
QRadioButton* QtGetRadioButton(wxRadioButton* radioBtn)
{
return static_cast<QRadioButton*>(radioBtn->GetHandle());
}
void QtStartNewGroup(QRadioButton* qtRadioButton)
{
// Note that the QButtonGroup created here will be deallocated when its
// parent // QRadioButton is destroyed.
QButtonGroup* qtButtonGroup = new QButtonGroup(qtRadioButton);
qtButtonGroup->addButton(qtRadioButton);
}
bool QtTryJoiningExistingGroup(wxRadioButton* radioBtnThis)
{
for ( wxWindow* previous = radioBtnThis->GetPrevSibling();
previous;
previous = previous->GetPrevSibling() )
{
if ( wxRadioButton* radioBtn = wxDynamicCast(previous, wxRadioButton) )
{
// We should never join the exclusive group of wxRB_SINGLE button.
if ( !radioBtn->HasFlag(wxRB_SINGLE) )
{
if ( QButtonGroup *qtGroup = QtGetRadioButton(radioBtn)->group() )
{
qtGroup->addButton(QtGetRadioButton(radioBtnThis));
return true;
}
}
break;
}
}
return false;
}
} // anonymous namespace
wxRadioButton::wxRadioButton() :
m_qtRadioButton(NULL)
@ -42,7 +89,27 @@ bool wxRadioButton::Create( wxWindow *parent,
m_qtRadioButton = new QRadioButton( parent->GetHandle() );
m_qtRadioButton->setText( wxQtConvertString( label ));
return QtCreateControl( parent, id, pos, size, style, validator, name );
if ( !QtCreateControl(parent, id, pos, size, style, validator, name) )
return false;
// Check if we need to create a new button group: this must be done when
// explicitly requested to do so (wxRB_GROUP) but also for wxRB_SINGLE
// buttons to prevent them implicitly becoming part of an existing group.
if ( (style & wxRB_GROUP) || (style & wxRB_SINGLE) )
{
QtStartNewGroup(m_qtRadioButton);
}
else
{
// Otherwise try to join an already existing group. Currently we don't
// do anything if joining it fails, i.e. if there is no current group
// yet, because the radio buttons not explicitly associated with some
// group still work as if they were part of one, so we just ignore the
// return value of this function.
QtTryJoiningExistingGroup(this);
}
return true;
}
void wxRadioButton::SetValue(bool value)