diff options
Diffstat (limited to 'src/quicktemplates2/qquickbuttongroup.cpp')
-rw-r--r-- | src/quicktemplates2/qquickbuttongroup.cpp | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/src/quicktemplates2/qquickbuttongroup.cpp b/src/quicktemplates2/qquickbuttongroup.cpp index 355fcf6a..2a1e354c 100644 --- a/src/quicktemplates2/qquickbuttongroup.cpp +++ b/src/quicktemplates2/qquickbuttongroup.cpp @@ -154,6 +154,8 @@ class QQuickButtonGroupPrivate : public QObjectPrivate Q_DECLARE_PUBLIC(QQuickButtonGroup) public: + QQuickButtonGroupPrivate() : exclusive(true) { } + void clear(); void buttonClicked(); void _q_updateCurrent(); @@ -163,6 +165,7 @@ public: static QQuickAbstractButton *buttons_at(QQmlListProperty<QQuickAbstractButton> *prop, int index); static void buttons_clear(QQmlListProperty<QQuickAbstractButton> *prop); + bool exclusive; QPointer<QQuickAbstractButton> checkedButton; QVector<QQuickAbstractButton*> buttons; }; @@ -188,6 +191,8 @@ void QQuickButtonGroupPrivate::buttonClicked() void QQuickButtonGroupPrivate::_q_updateCurrent() { Q_Q(QQuickButtonGroup); + if (!exclusive) + return; QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton*>(q->sender()); if (button && button->isChecked()) q->setCheckedButton(button); @@ -244,9 +249,12 @@ QQuickButtonGroupAttached *QQuickButtonGroup::qmlAttachedProperties(QObject *obj /*! \qmlproperty AbstractButton QtQuick.Controls::ButtonGroup::checkedButton - This property holds the currently selected button, or \c null if there is none. + This property holds the currently selected button in an exclusive group, + or \c null if there is none or the group is non-exclusive. + + By default, it is the first checked button added to an exclusive button group. - By default, it is the first checked button added to the button group. + \sa exclusive */ QQuickAbstractButton *QQuickButtonGroup::checkedButton() const { @@ -306,6 +314,38 @@ QQmlListProperty<QQuickAbstractButton> QQuickButtonGroup::buttons() } /*! + \since QtQuick.Controls 2.3 (Qt 5.10) + \qmlproperty bool QtQuick.Controls::ButtonGroup::exclusive + + This property holds whether the button group is exclusive. The default value is \c true. + + If this property is \c true, then only one button in the group can be checked at any given time. + The user can click on any button to check it, and that button will replace the existing one as + the checked button in the group. + + In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; + instead, another button in the group must be clicked to set the new checked button for that group. + + In a non-exclusive group, checking and unchecking buttons does not affect the other buttons in + the group. Furthermore, the value of the \l checkedButton property is \c null. +*/ +bool QQuickButtonGroup::isExclusive() const +{ + Q_D(const QQuickButtonGroup); + return d->exclusive; +} + +void QQuickButtonGroup::setExclusive(bool exclusive) +{ + Q_D(QQuickButtonGroup); + if (d->exclusive == exclusive) + return; + + d->exclusive = exclusive; + emit exclusiveChanged(); +} + +/*! \qmlmethod void QtQuick.Controls::ButtonGroup::addButton(AbstractButton button) Adds a \a button to the button group. @@ -326,7 +366,7 @@ void QQuickButtonGroup::addButton(QQuickAbstractButton *button) QObjectPrivate::connect(button, &QQuickAbstractButton::clicked, d, &QQuickButtonGroupPrivate::buttonClicked); QObjectPrivate::connect(button, &QQuickAbstractButton::checkedChanged, d, &QQuickButtonGroupPrivate::_q_updateCurrent); - if (button->isChecked()) + if (d->exclusive && button->isChecked()) setCheckedButton(button); d->buttons.append(button); |